'ViewModelProviders' is deprecated from androidx.lifecycle:lifecycle-*:2.2.0-alpha03 https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0-alpha03 Here is solution for Kotlin 'ViewModelProviders' is deprecated. After upgrading lifecycle-extensions:2.1.0 to 2.2.0-alpha05 But what is Java version of that? ViewModelProvider(this).get(MyViewModel.class) not work for Java.
Asked
Active
Viewed 2,210 times
2
-
They are replacing it with `new ViewModelProvider(this)` for some reason. Tbh the previous variant was better, but it's their lib – EpicPandaForce Dec 07 '19 at 08:02
-
Yes, that's correct. Thanks mate. – A.M. Dec 07 '19 at 08:45
3 Answers
5
ViewModelProviders.of(this).get(MyViewModel.class); - deprecated
new ViewModelProvider(this).get(MyViewModel.class); - correct
Thanks @EpicPandaForce !

A.M.
- 215
- 1
- 10
1
Generic Example (up to android.arch.lifecycle:extensions:1.1.0). it is deprecated from 1.1.1,
MyViewModel myViewModel = new ViewModelProviders.of(this, new MyViewModelFactory(this.getApplication(), "Your string parameter")).get(MyViewModel.class);
Generic Example (for android.arch.lifecycle:extensions:1.1.1)
MyViewModel myViewModel = new ViewModelProvider(this, viewModelFactory).get(MyViewModel.class);
OR, Use ViewModelStore link
MyViewModel myViewModel = new ViewModelProvider(getViewModelStore(), viewModelFactory).get(MyViewModel.class);

Chirag Bhuva
- 851
- 7
- 14
0
You can also create a CREATOR object for your ViewModel and call this object from your activity this object will act as a factory object that you can call as you call any ViewModelFactory.
ViewModelProviders.of(this, ViewModelFactoryObject).get(ViewModel::class.java).apply{}

Ishtdeep Hora
- 310
- 2
- 4