OnLoadFinished is not called after returning from the HOME button

When using a custom AsyncTaskLoader to load data from a web service, if I press the HOME button in the middle of the download process and run the application again, the onLoadFinished () method will not be called. My snippet calls setRetainInstance(true) in onActivityCreated() and also calls getLoaderManager.initLoader(0, null, this) in the same method (as recommended).

During testing, I see that when returning to the fragment, onActivityCreated() not called, so maybe therefore onLoadFinished() not called. But where else to put the initLoader() method? I read in several places that it cannot be called in onResume() .

So, any ideas? I have many downloaders on different screens of my application and I need to solve this problem in an elegant way.

+8
android fragment
source share
1 answer

After addressing issue 14944 ( http://code.google.com/p/android/issues/detail?id=14944 ), I solved the problem by overriding onStartLoading() in my custom AsyncTaskLoader and called forceLoad() .

An even better solution is to create a custom parent AsyncTaskLoader that looks like this (taken from the alexvem suggestion from the link above):

 public abstract class AsyncLoader<D> extends AsyncTaskLoader<D> { private D data; public AsyncLoader(Context context) { super(context); } @Override public void deliverResult(D data) { if (isReset()) { // An async query came in while the loader is stopped return; } this.data = data; super.deliverResult(data); } @Override protected void onStartLoading() { if (data != null) { deliverResult(data); } if (takeContentChanged() || data == null) { forceLoad(); } } @Override protected void onStopLoading() { // Attempt to cancel the current load task if possible. cancelLoad(); } @Override protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); data = null; } } 
+22
source share

All Articles