Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I refresh my ListFragment when it returns to the layout from back stack?
    text
    copied!<p>First I should mention that I am using the ActionBarSherlock library for backwards compatibility. </p> <p>I have an activity which adds a <code>ListFragment</code> when it is first started. I have a custom <code>Loader</code> which I implemented and follows the <a href="http://developer.android.com/reference/android/content/AsyncTaskLoader.html" rel="noreferrer">AsnycTaskLoader example</a> very closely. My <code>ListFragment</code> implements the <code>LoaderCallbacks&lt;Cursor&gt;</code> interface. All the appropriate callback methods are called when the fragment is added (<code>onCreateLoader()</code> , <code>onLoaderFinished()</code> ) and when it is replaced (<code>onLoaderReset()</code> ). </p> <p>My <code>onActivityCreated(Bundle)</code> method looks like this:</p> <pre><code>@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); } </code></pre> <p>Later on, the <code>ListFragment</code> 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 <code>android:empty</code> elements are displayed and none of the LoaderCallback methods are called. I can use the debugger to determine that <code>getLoaderManager().initLoader(0, null, this);</code> is actually called, but nothing else. When I change it to <code>getLoaderManager().restartLoader(0, null, this);</code>, the callbacks get called, but still my list remains empty (although there is data, the view is not refreshed). </p> <p>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?</p> <p>FYI, here are my callback methods</p> <pre><code> @Override public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) { return new AccountsCursorLoader(this.getActivity() .getApplicationContext()); } @Override public void onLoadFinished(Loader&lt;Cursor&gt; loaderCursor, Cursor cursor) { mCursorAdapter.swapCursor(cursor); mCursorAdapter.notifyDataSetChanged(); } @Override public void onLoaderReset(Loader&lt;Cursor&gt; arg0) { mCursorAdapter.swapCursor(null); } </code></pre> <p>Some notes:</p> <ol> <li>I cannot use the <code>setListShown(true)</code> methods in the example because I get an <code>IllegalStateException</code> that it cannot be used with a custom content view.</li> <li>My <code>AccountsCursorAdapter</code> extends a <code>SimpleCursorAdapter</code> and modifies only the <code>bindView()</code> method.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload