简体   繁体   中英

Loader is still alive after onDestroy

So from the documentation for Loaders it says that LoaderManager destroys the loaders when the Activity or Fragment is destroyed, but the activity callback onDestoy() is called when the screen is rotated. But the loader still retains it's data somehow. Can anybody explain when the loader is destroyed and when it retains data.

If you visit the Android developers documentation you will find this text that gives answer to your question.

Loaders run on separate threads to prevent janky or unresponsive UI. Loaders simplify thread management by providing callback methods when events occur. Loaders persist and cache results across configuration changes to prevent duplicate queries.

Source: https://developer.android.com/guide/components/loaders.html

Same as the FragmentManager recreates it's fragments during configuration change automatically and saves the values from the widgets with unique id like EditText, Loaders persist their results during configuration change to prevent duplicate queries which will waste resources and time.

The Android documentation states that Loaders save you the work of dealing with Fragment and Activity lifecycle :

If you fetch the data from another thread, perhaps with AsyncTask, then you're responsible for managing both the thread and the UI thread through various activity or fragment lifecycle events, such as onDestroy() and configurations changes.

I guess the Loader API is meant for that sort of behaviour (retaining data when rotating the screen, pausing the activity etc..)

Source: https://developer.android.com/guide/components/loaders.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM