Android ViewModel. How it works.

ViewModel 2017

Honestly, this part (I mean exactly ViewModel’s version of 2017) can be unnecessary, because Medium has several articles, that describes viewModel saving state mechanism in first version. I like this one. But I suggest you more code than there.

The ViewModel class allows data to survive configuration changes such as screen rotations.

It can lead to research this class.

ViewModel class
ViewModelProviders methods of()
ViewModelProvider method get(modelClass)
ViewModelProvider method get(key, modelClass)
ViewModelStores method of()
HolderFragment methods holderFragmentFor()
HolderFragment declaration

ViewModel 2018

As was mentioned before: not all activities and fragments implemented ViewModelStoreOwner. But in summer of 2018 the massive migration on AndroidX was performed. Now FragmentActivity and Fragment become ViewModelStoreOwner implementations. If so, why does framework need holderFragments?

Commit for removing HolderFragment
Commit for removing HolderFragment
ViewModelProviders methods of() in 2018

Saving ViewModelStore instance for Activity

First of all — a whole scheme of interactions to get ViewModel instance for Activity in 2018.

Getting ViewModel instance for Activity in 2018
FragmentActivity method getViewModelStore()
/**
* Retrieve the non-configuration instance data that was previously
* returned by {
@link #onRetainNonConfigurationInstance()}. This will
* be available from the initial {
@link #onCreate} and
* {
@link #onStart} calls to the new instance, allowing you to extract
* any useful dynamic state from the previous instance.
*
* ...
*/
Activity method getLastNonConfigurationInstance()
/**
* Called by the system, as part of destroying an
* activity due to a configuration change, when it is known that a new
* instance will immediately be created for the new configuration. You
* can return any object you like here, including the activity instance
* itself, which can later be retrieved by calling
* {
@link #getLastNonConfigurationInstance()} in the new activity
* instance.
*
* ...
*
*
@return any Object holding the desired state to propagate to the
* next activity instance
*/
FragmentActivity method onRetainNonConfigurationInstance()
Retrieving viewModelStore instance when Activity created first time
Destroying Activity
Creating a new instance of Activity
FragmentActivity alternatives for methods …NonConfigurationInstance()

Saving ViewModelStore instance for Fragment

WARNING! This part of article will hide most of code by schemes, because some methods are quite big and have a lot of details, and many of them are not important for saving instances of ViewModel. Use Android framework source code if you would like to see the code.

ViewModel initialization for Fragment
Getting ViewModel instance for Fragment in 2018
Saving ViewModelStore instance for Fragment
Saving ViewModelStore instance for Fragment
Retaining FragmentManagerNonConfig inside method onRetainNonConfigurationInstance()
Restoring ViewModelStore instance for Fragment

ViewModel 2019

First of all, please allow to thank you. You have deserved a candy for your patience and desire to find out all secrets of saving states within Android ViewModel. You are almost getting the goal.

ViewModel class in 2019
Process of saving and restoring viewModel stores in case of Activity (2019)
Method getViewModelStore() in FragmentManagerImpl
Process of saving and restoring viewModel stores in case of Fragment (2019)
Initialization of FragmentManagerViewModel
Method attachController() in FragmentManagerImpl
Initialization of FragmentController inside FragmentActivity

All right, all right, man! And so what?

Yes, this article covered mechanism of saving states, using Android ViewModel. All versions of it. It is good, but what is so useful in this big text?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store