Search code examples
androidandroid-loadermanagerandroid-loader

When an Activity instance is destroyed during a configuration change, are the Loaders associated with it also destroyed?


From this developer guide on Loaders,

They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.

I assume that by "last loader", they mean that the loader which was destroyed when the Activity was destroyed during the configuration change. Then by saying "last loader's cursor", they mean the cursor which was associated with the last loader. This tells us that when an Activity is destroyed because of a configuration change, its loaders are also destroyed, but the cursors (or any other form of data that the loader has loaded) is not destroyed. Is that correct?

Actually it is a little difficult to internalize this. My intuition says that when the loader is destroyed, everything associated with it, including the cursor associated with it should also be destroyed.


Solution

  • The Loader instance is kept alive during configuration change. The LoaderCallbacks of the old Activity is disconnected and the new one reconnected.

    Take a look at LoaderManager.java around the retain() and finishRetain() methods of LoaderInfo. You can see that the mCallbacks field is nulled, but the mLoader field is not.

    The initialization of these fields happens in LoaderInfo.start(), where you can see that mCallbacks.onCreateLoader() is only invoked if mLoader is null.