ViewModelProviders is deprecated in 1.1.0

I use lifecycle-extensions 2.2.0 version:

implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" 

It should work, using ViewModelProvider constructor.

// With ViewModelFactory   
val viewModel = ViewModelProvider(this, YourViewModelFactory).get(YourViewModel::class.java)


//Without ViewModelFactory
val viewModel = ViewModelProvider(this).get(YourViewModel::class.java)

2020/5/15 Update

I found another elegant way to achieve this, Android KTX can help

implementation "androidx.fragment:fragment-ktx:1.2.4"
val viewmodel: MYViewModel by viewModels()
val viewmodel: MYViewModel by viewModels { myFactory } //With factory

Ref: https://developer.android.com/reference/kotlin/androidx/fragment/app/package-summary#viewmodels

2020/06/25: corrected the case of the delegate

Leave a Comment