Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8075495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:00:19+00:00 2026-06-05T15:00:19+00:00

First I should mention that I am using the ActionBarSherlock library for backwards compatibility.

  • 0

First I should mention that I am using the ActionBarSherlock library for backwards compatibility.

I have an activity which adds a ListFragment when it is first started. I have a custom Loader which I implemented and follows the AsnycTaskLoader example very closely. My ListFragment implements the LoaderCallbacks<Cursor> interface. All the appropriate callback methods are called when the fragment is added (onCreateLoader() , onLoaderFinished() ) and when it is replaced (onLoaderReset() ).

My onActivityCreated(Bundle) method looks like this:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mAccountsDbAdapter = new AccountsDbAdapter(getActivity().getApplicationContext());

    setHasOptionsMenu(true);
    mCursorAdapter = new AccountsCursorAdapter(getActivity()
            .getApplicationContext(), R.layout.list_item_account, null,
            new String[] { DatabaseHelper.KEY_NAME },
            new int[] { R.id.account_name }, 0);

    setListAdapter(mCursorAdapter); 
    getLoaderManager().initLoader(0, null, this);
}

Later on, the ListFragment is replaced with another Fragment B.
When the user presses the back button, Fragment B is removed and the ListFragment is added again. However, the list is empty and only the android:empty elements are displayed and none of the LoaderCallback methods are called. I can use the debugger to determine that getLoaderManager().initLoader(0, null, this); is actually called, but nothing else.
When I change it to getLoaderManager().restartLoader(0, null, this);, the callbacks get called, but still my list remains empty (although there is data, the view is not refreshed).

How can I get my ListFragment to refresh itself when it is returned to the layout?
Has anyone encountered this before, how did you fix it?

FYI, here are my callback methods

    @Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return new AccountsCursorLoader(this.getActivity()
            .getApplicationContext());
}

@Override
public void onLoadFinished(Loader<Cursor> loaderCursor, Cursor cursor) {
    mCursorAdapter.swapCursor(cursor);
    mCursorAdapter.notifyDataSetChanged();
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    mCursorAdapter.swapCursor(null);
}

Some notes:

  1. I cannot use the setListShown(true) methods in the example because I get an IllegalStateException that it cannot be used with a custom content view.
  2. My AccountsCursorAdapter extends a SimpleCursorAdapter and modifies only the bindView() method.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:00:20+00:00Added an answer on June 5, 2026 at 3:00 pm

    Eureka!!! I found it (by accident, of course)

    TL;DR;

    In the AccountsListFragment, I now create my database adapter (AccountsDatabaseAdapter) in the onCreate() method and close it in the onDestroy() method and it now works. Previously I was creating my adapter in the onActivityCreated() and closing in onDestroyView(). Note that I am not referring to the ListAdapter, but rather to my database interfacing AccountsDbAdapter.

    Attempt at long explanation:

    Well, I was doing this because I thought that getting context through getActivity() will not be possible in the onCreate() method. It turns out you can getActivity() even before onActivityCreated() is called.

    But I cannot really explain why this now works because the Loader has its own DatabaseAdapter object which it uses to retrieve the data. If I were to guess, I would say that the same database object is returned for both database adapters (the database is cached). Which would mean that when I close one in onDestroyView(), the other is also closed and the cursor data set becomes invalid which results in an empty list view. The loader does not reload because it thinks the data has not changed.

    But even this explanation does not satisfy me completely, because some of the suggested solutions here which I tried like force restarting the loader each time did not work. (in the loadInBackground() method, a new DatabaseAdapter is created each time).

    Anyway, if you happen to have a better understanding of what is going on, please hammer away in the comments.

    Thanks everyone for the help with this!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I should say that I don't have any experience in working with
I have created a file argument.pl which takes several arguments first of which should
First of all, I should probably mention that I'm pretty new to Java. I'm
At first I should mention that this problem only occurs in windows forms applications
I have a feeling that this should really not be all that difficult, yet
First, let me just mention that this is my first attempt at a from-the-ground-up
I've read through scores of questions on here that seem at first to have
Maybe I should first give an idea of what I want to accomplish as
The above expression is working fine. this expression means first leter should start with
I don't understand what lifting is. Should I first understand monads before understanding what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.