Answer the question
In order to leave comments, you need to log in
Activity lifecycle, how is data cached when config changes?
I decided to move away from Loader and in the future from fragments in Android, and in the process of rethinking, the question arose of how AsyncTaskLoader caches data? After changing the configuration, we initialize the Loader (initLoader) and if it already exists and it previously had some data returned through deliverResult, then it returns them again.
After digging into the sources of fragments and loaders, I realized that when changing the configuration, LoaderManager saves all existing loaders, and then restores them back. That is, the Activity controls the life cycle of all Fragments through the FragmentManager, which in turn controls the Loader life cycle through the LoaderManager.
In the end, I want to ask those in the know. Do I understand correctly that an Activity instance is created at the OS level, which, when the configuration is changed, is not destroyed, but untied from the context, and then attached to a new context, and therefore all fields and objects inside the Activity remain until onDestroy is called? And if I'm wrong, how does the Activity store the LoaderManager when the configuration changes? Hardly through static variables. Can I bind to the Activity's lifecycle to store cached session data of my loader there, which I'll destroy according to the Activity's lifecycle?
PS I don't want to use static variables and singletons.
PSS Already after writing the question, I found in the Activity source a bunch of classes for storing objects that do not change when the configuration changes:
/*Activity.java*/
static final class NonConfigurationInstances {
Object activity;
HashMap<String, Object> children;
List<Fragment> fragments;
ArrayMap<String, LoaderManager> loaders;
VoiceInteractor voiceInteractor;
}
/* package */ NonConfigurationInstances mLastNonConfigurationInstances;
Answer the question
In order to leave comments, you need to log in
Searching the internet, I found the answer to my question.
blog.bradcampbell.nz/mvp-presenters-that-survive-c...
This guy creates a Cache in which he stores all the current views, adds new ones and removes old ones. If necessary, all views are formed into an object, which is given to the Activity for storage through the onRetainCustomNonConfigurationInstance
. After the Activity has been recreated, you need to get the saved object with the help getLastCustomNonConfigurationInstance
and load it all back into the Cache view manager.
there is no magic.
not at all.
Yes, it seems to many that if they write android:configChanges="orientation|screenSize" in the manifest, a good wizard will say crib-crab-booms and everything will be fine, but it's not ...
either loaders think that your data is saved when changing orientation ( although in the first place they are not for this) something else, but in all cases you yourself, depending on your architecture and
tools
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question