This might seem like a silly question but I can't wrap my head around AsynctaskLoader's status.
I've read the documentation several times, as well as other tutorials on the web. However, I still can't understand when an Asynctask is in the Reset State
Take a look a this bit of code extracted from the official documentation. http://developer.android.com/reference/android/content/AsyncTaskLoader.html
@Override public void deliverResult(List<AppEntry> apps) {
if (isReset()) {
// An async query came in while the loader is stopped. We
// don't need the result.
if (apps != null) {
onReleaseResources(apps);
}
}
This method is called then the Loader is done with its work and needs to send the data back to the UI thread. My question is: why are we asking if the Loader is Reset?. What does it even mean for it to be reset? The class says
/**
* Return whether this load has been reset. That is, either the loader
* has not yet been started for the first time, or its {@link #reset()}
* has been called.
*/
You'd think that if the loader hasn't been started for the first time, it'd be in the STOPPED stated, why restarted?.
I guess I'm not really understanding how the Loader reacts to the Activity Lifecycle, but the documentation says nothing about it.

I Think this will help you. This map show both android life cycle and loader life cycle.
"However, I still can't understand when an Asynctask is in the Reset State"
I believe you meant "...when the LOADER is in the Rest State"
Normally one would use the LoaerManager to manage the life-cycle of loaders.
When the user calls LoaderManager#destroyLoader() the LoadManager removes this loader from it's cache. A loader may also be destroyed when LoaderManager#restartLoader() is called or when the Activity/Fragment goes through it's destroy phase. If such loader previously delivered data to its client (normally Fragment or Activity), then the LoaderManager will call onLoaderReset() and will instruct the loader to reset. This provides the client an opportunity to remove any references to the data and for the loader to release any resources associated with the data. Remeber that the owner of the data is the loader and not the client.
"My question is: why are we asking if the Loader is Reset?"
deliverResult is called in the context of the UI thread but is triggered by the completion of the background thread that does the actual loading. It is possible for the loader state to be changed to reset() prior to the completion of the background thread. The check isReset() is performed to avoid notifying the client of new data in this race condition situation where the loader is in reset state.
"What does it even mean for it to be reset?"
A loader in reset state should stop all its operations since it may be destroyed shortly after. In particular it should release any resources occupied with previous loaded data, stop loading new data and monitoring for change in the underline data source.
"You'd think that if the loader hasn't been started for the first time, it'd be in the STOPPED stated, why restarted?."
Stopped state indicates that the loader was previously in started state. In stopped state, the loader may have data that was previously loaded and it should monitor for changes in the underline data source. From Stopped state the loader can go back to started state or to reset state. If it is restarted, then it may use its previous loaded data if no changes in there weren't any changes in the data source while it was stopped.
When a loader is created but before it is started it is considered in a reset state. This works well because in this state there is no loaded data yet and also the loader shouldn't perform any operations as mentioned above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With