What is ViewModelStore and viewModelStoreOwner?

Can anyone define them and how to use them and what they really mean to us developer’s?

A ViewModelStore can be considered as a container that stores the ViewModels in a HashMap. Where the key is string value and value is the ViewModel being saved(ViewModelProvider uses a concatenation of the string_key + ViewModel class canonical name).

A ViewModelStoreOwner is merely an interface. Any class that implements the getViewModelStore() defined by this interface becomes the owner of ViewModelStore. This class then maintains the ViewModelStore and should be responsible to appropriately restoring it when needed.

We can implement our own version of the owner and the state based on the requirement.

is ViewModelStoreOwner==activity/fragment?

Yes. Based on the Android source code, both Fragment (from androidx.fragment.app) & ComponentActivity (from androidx.activity) implements ViewModelStoreOwner. These classes maintains a viewModelStore and value is restored appropriately.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)