In my current project I use the next line:
mViewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
For instance a ViewModel
but in https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders.html#ViewModelProviders() recommend use ViewModelProvider.AndroidViewModelFactory
because ViewModelProviders()
was deprecated in API level 1.1.0.
any idea for this purpose?
Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider , passing in the fragment. This function is deprecated. Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider , passing in the activity. of (fragment: Fragment, factory: 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.
What is ViewModel Factory? The factory is responsible to create your instance of ViewModel. If your ViewModel has dependencies and you want to test your ViewModel then you should create your own ViewModelProvider. Factory and passed dependency through ViewModel constructor and give value to the ViewModelProvider.
If you had a simple ViewModel extending AndroidViewModel without any additional constructor parameters, its as follows
- Extend AndroidViewModel without any additional constructor parameters
class FooViewModel(application: Application) : AndroidViewModel(application) {}
- Create View Model in Activity
val viewModel = ViewModelProvider(this).get(FooViewModel::class.java)
But if you had a ViewModel extending AndroidViewModel with any additional constructor parameters, its as follows
- Extend AndroidViewModel with any additional constructor parameters
class FooViewModel(application: Application, foo: Foo) : AndroidViewModel(application) {}
- Create a new view model factory extending ViewModelProvider.AndroidViewModelFactory
class FooViewModelFactory(val application: Application, val foo: Foo): ViewModelProvider.AndroidViewModelFactory(application) {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return FooViewModel(
application, foo
) as T
}
}
- Create View Model in Activity
val viewModel = ViewModelProvider(this, FooViewModelFactory(application, foo)).get(FooViewModel::class.java)
EDIT: The original question is now irrelevant, as you should no longer use the ViewModelProviders
utility class. Instead, you should create a ViewModelProvider
instance like so:
val viewModel = ViewModelProvider(thisFragment).get(MyViewModel::class.java)
Original answer below.
ViewModelProviders
is just a utility class with static methods, there's no need to instantiate it (there are no instance methods in it anyway), so the constructor being deprecated shouldn't be a concern.
The way you use it is by calling its appropriate of
method for your use case, passing in a Fragment
or Activity
, and then calling get
on the ViewModelProvider
it returns:
val viewModel = ViewModelProviders.of(thisFragment).get(MyViewModel::class.java)
If you don't provide your own factory in the second parameter of the of
method, AndroidViewModelFactory
will be used by default. This implementation can either create ViewModel subclasses that have no constructor parameters, or ones that extend AndroidViewModel
, like such:
class MyViewModel(application: Application) : AndroidViewModel(application) {
// use application
}
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