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).
public ViewModelProvider(@NonNull ViewModelStoreOwner owner) { this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory ? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory() : NewInstanceFactory.getInstance()); }
public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) { this(owner.getViewModelStore(), factory); }
public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) { mFactory = factory; mViewModelStore = store; }
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc02"
So These Constructor's require ViewModelStore
and viewModelStoreOwner
.
@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?
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.
ViewModel and ViewModelFactory ViewModel is class to store and manage UI-related data, it allow data survive in device configure saturation (like screen rotation).
Creates ViewModelProvider , which will create ViewModels via the given Factory and retain them in the given store . ViewModelStore : ViewModelStore where ViewModels will be stored.
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).
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With