Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ViewModelStore and viewModelStoreOwner?

I am very confused due to this new ViewModelProvider api(ViewModelProviders is deprecated)

As with the new changes there are new Constructors also (Source code).

#1

public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {         this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory                 ? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory()                 : NewInstanceFactory.getInstance());     } 

#2

public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) {         this(owner.getViewModelStore(), factory);     } 

#3

 public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {         mFactory = factory;         mViewModelStore = store;     } 

Gradle Depenedency:

implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc02" 

So These Constructor's require ViewModelStore and viewModelStoreOwner.

Doc:

@param store {@code ViewModelStore} where ViewModels will be stored.

@param owner a {@code ViewModelStoreOwner} whose {@link ViewModelStore} will be used to retain {@code ViewModels}


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


is ViewModelStoreOwner==activity/fragment?

like image 909
Anmol Avatar asked Nov 16 '19 15:11

Anmol


People also ask

What is ViewModelStore?

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.

What is the ViewModelFactory interface for?

ViewModel and ViewModelFactory ViewModel is class to store and manage UI-related data, it allow data survive in device configure saturation (like screen rotation).

What is the use of ViewModelProvider?

Creates ViewModelProvider , which will create ViewModels via the given Factory and retain them in the given store . ViewModelStore : ViewModelStore where ViewModels will be stored.

How do I get ViewModelStoreOwner in fragment?

To share the objects among fragments, simply use the activity instance for creating ViewModelProvider which will use the same ViewModelStoreOwner in all fragments hence will return the persisted object of viewmodel (if created before).

What is a viewmodelstore in Salesforce?

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).

How do I get the viewmodelstore of a class in Java?

ViewModelStoreOwner is a simple interface with a single method named getViewModelStore () In the constructor, we are simply getting the ViewModelStore from ViewModelStoreOwner and passing it to the other constructor where they are just assigned to the respective class members.

What is the difference between viewmodelprovider and viewmodelstoreowner?

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.

Where are the ViewModel instances stored?

If the viewmodel is already present, it simply returns the viewmodel instance present in ViewModelStore and if it’s not present, the ViewModelProvider uses the Factory instance to create a new viewmodel object and also stores it in ViewModelStore. Now we know that our activity is responsible for storing the ViewModel instances.


1 Answers

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.

like image 64
Sagar Avatar answered Oct 06 '22 09:10

Sagar