36

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?

Anmol
  • 8,110
  • 9
  • 38
  • 63

1 Answers1

33

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.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • 7
    Can the `Application` class be a ViewModelStoreOwner? – IgorGanapolsky Aug 10 '20 at 16:24
  • 1
    I have successfully made my ```CustomApplication``` (which inherits from ```Application```), implement the ```ViewModelStoreOwner``` interface. And later, I've been able to use my ```CustomApplication``` as the ```ViewModelStoreOwner``` of a ```ViewModel``` which later can be easily shared accross different ```Activity```es and/or ```Fragment```s. – xarlymg89 Feb 10 '21 at 15:41
  • @xarlymg89 can you please share how? More specifically how did you implement ViewModelStoreOwner – dejavu89 May 11 '21 at 22:26
  • 3
    @dejavu89 ```ViewModelStoreOwner``` is just an interface. You simply have to implement their methods. ```class MyApplication : ViewModelStoreOwner { private val appViewModelStore: ViewModelStore by lazy { ViewModelStore() } override fun getViewModelStore(): ViewModelStore { return appViewModelStore } }``` – xarlymg89 May 12 '21 at 06:18
  • 1
    Well, I know it's been an old post but the thing that bothered me that once you make a component say application class a viewmodelStoreOwner by implementing its method then you have taken the responsibility of retaining viewmodelStore during configurations change and you have to clear the viewmodelStore once viewmodelStoreOwner is going to be destroyed. Without it there is no point of making a component viewmodelStoreOwner – Vivek Gupta Sep 20 '22 at 03:20