What is the difference between ViewModelProviders and ViewModelProvider class?
Asked Answered
T

3

25

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?

Tommie answered 21/1, 2019 at 12:1 Comment(1)
ViewModelProviders is deprecated. Use ViewModelProvider(this).get(YourViewModel::class.java).Ammoniacal
S
23

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.

Edit:

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(SomeViewModel::class.java)

Stratum answered 21/1, 2019 at 12:27 Comment(3)
Which api should we use then?Bract
@Bract If you're using latest alpha dependency of ViewModel then this API has changed how we used to get ViewModels from ViewModelProviders.Stratum
@JeelVankhede need your help here #64422465Progestational
A
0

The ViewModelProviderS class provides instances of ViewModelProvider class.Your call to ViewModelProviders.of("this") creates and returns a ViewModelProvider associated with the activity.

ViewModelProvider on the other hand provides instances of ViewModel of the Activity. Calling the ViewModelProviders.get(ExampleViewModel::class.java) returns an instance of ExampleViewModel class

These functions usually go together like:

ViewModelProviders.of("this").get(ExampleViewModel::class.java)
Anode answered 10/1, 2020 at 16:5 Comment(0)
P
0

@andritK, it seems ViewModelProviders is deprecated now. The docs says we can straight away use ViewModelProvider in this case now. then how about changing below codes to the following: FROM

inline fun <reified T : ViewModel> Fragment.viewModel(factory: ViewModelProvider.Factory, body: T.() -> Unit): T {
    val vm = ViewModelProviders.of(this, factory)[T::class.java]
    vm.body()
    return vm
}

TO

inline fun <reified T : ViewModel> Fragment.viewModel(factory: ViewModelProvider.Factory, body: T.() -> Unit): T {
    val vm = ViewModelProvider(this, factory).get(T::class.java)
    vm.body()
    return vm
}

Does this work?

Phenomenalism answered 2/2, 2020 at 18:34 Comment(1)
What is the need for reified?Bract

© 2022 - 2024 — McMap. All rights reserved.