0

I have search results displayed using ViewPager & FragmentStatePageAdapter, with each set of results in its own fragment. THe data is loaded using a custom AsyncTaskLoader. I want to preserve the scroll position when going to some other activity / to background and coming back.

However, on navigation to another activity and coming back, the Loader Managers calls the onStartLoading() of the loader and unnecessarily reloads the data:

protected void onStartLoading() {
    if (results.size() > 0) {
        Log.d(TAG, "onStartLoading() - deliver result");
        deliverResult(mResults);
    }

    if (takeContentChanged() || results.size() == 0) {
        Log.d(TAG, "onStartLoading() - force load");
        // Needed for custom loaders
        forceLoad();
    }

 // Register observer...

}

This also means that I cannot restore the scroll position (saved on fragment's on save). Any way to avoid this reload on fragment's onResume? I am calling the initLoader() only in onActivityCreated(), which is not called when the fragment is restored from the backstack.

I am using the v4 compatibility lib.

Thanks!

Hari
  • 401
  • 4
  • 10
  • How are you populating the ListView? Let's see your onLoadFinished() and onLoaderReset() calls in your fragment/activity. – kwazi Apr 17 '13 at 14:26
  • onLoadFinished() calls fillAdapter after checking the search keyword hasn't changed. My onLoaderReset() just clears the adapter. – Hari Apr 18 '13 at 20:19
  • Just noticed this too... Very weird. :/ Had to prevent this with a boolean that ignored the result in onLoadFinished(). – Patrick Aug 02 '13 at 06:58

1 Answers1

0

A small hint to check ... when you initLoader it might not call onStartLoading but it will for sure call onLoadFinished (and deliver old results).

Maybe that is happening to you?

Also try reading this thread: Why is onLoadFinished called again after fragment resumed?