What Is The Difference Between ViewModelProviders And ViewModelProvider Class?

- 1 answer

I saw two classes with a similar name, ViewModelProviders, and ViewModelProvider. Can anyone explain what are the difference between these classes? which class actually provide the ViewModel?



ViewModelProviders(belongs to Maven artifact android.arch.lifecycle:extensions) is a class from android.arch.lifecycle package which contains utilities methods for ViewModelStore class & returns you object of ViewModelProvider class when you use of() method from it.

So, you can think of as wrapper around library class for ViewModelProvider.

On the other hand, ViewModelProvider(belongs to Maven artifact android.arch.lifecycle:viewmodel) is class that provides ViewModels for a scope. So it's default ViewModelProvider for an Activity or a Fragment can be obtained from ViewModelProviders class.

So, yes ! this is the main class that provides core logic for your ViewModel, but you'll need to obtain it from ViewModelProviders which returns you this class to obtain ViewModel from.


After ViewModel version 2.2.0:

Class ViewModelProviders has been deprecated and we can now use ViewModelProvider directly by creating it's new instance for getting ViewModel.

Something like this: ViewModelProvider(this).get(