D
D
Deadkenny2016-02-09 12:08:07
Android
Deadkenny, 2016-02-09 12:08:07

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;

It follows from this that the OS still destroys the Activity.
TL;DR I use MVP. How do I bind my Presenter's data caching without binding to Fragments, not using a database, not storing data in sharedpreferences, but only storing the cache as objects and using the Activity's lifecycle and methods?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Deadkenny, 2016-02-09
@Deadkenny

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 getLastCustomNonConfigurationInstanceand load it all back into the Cache view manager.

O
Oleg Gamega, 2016-02-09
@gadfi

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

L
lomikman, 2016-02-09
@lomikman

Try using dagger2.
Where you need to use your presenter, you do
@Inject Presenter presenter
where your presenter class can be written @Singleton and then one instance will be used for the entire app and you can reuse it anywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question