Looking at the Google docs for ViewModel
, they show the below sample code on how to get a ViewModel
:
val model = ViewModelProviders.of(this).get(MyViewModel::class.java)
When using the latest dependency android.arch.lifecycle:extensions:1.1.1
there is no such class ViewModelProviders
.
Going to the documentation for ViewModelProviders
, I saw a comment saying:
This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory
The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory
, cannot find an equivalent of
method to get the instance of the ViewModel
.
What i tried doing:
ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(PlayerViewHolder::class.java)
Hence the name of the method create
, I get a new instance of the ViewModel every-time I call it, which is not what I am after.
Any ideas what is the replacement of deprecated code above?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…