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
}
ViewModel
instance? – Amick