[英]As ViewModelProviders.of() is deprecated, how should I create object of ViewModel?
I have been trying to create an Object of ViewModel in an Activity but ViewModelProviders is deprecated So what's the alternative to create the ViewModel's object.我一直在尝试在 Activity 中创建 ViewModel 的 Object 但不推荐使用 ViewModelProviders 那么创建 ViewModel 的 object 的替代方法是什么。
Simply replace:只需更换:
This:这个:
boardViewModel = ViewModelProviders.of(this).get(BoardViewModel::class.java)
With this:有了这个:
boardViewModel = ViewModelProvider(this).get(BoardViewModel::class.java)
This Gradle upgrade created the problem for me.这个 Gradle 升级给我带来了问题。
FROM从
implementation 'androidx.core:core:1.1.0'
TO至
implementation 'androidx.core:core:1.2.0'
IN MAIN ACTIVITY Java/Kotlin Files主要活动 Java/Kotlin 文件
This import statement这个导入语句
import androidx.lifecycle.ViewModelProviders
had to be changed to必须改为
import androidx.lifecycle.ViewModelProvider
This KOTLIN viewModel statement这个 KOTLIN viewModel 语句
viewModel = ViewModelProviders.of(this).get(MainActivityViewModel::class.java)
had to be changed to必须改为
viewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)
and in JAVA在 JAVA 中
This line of JAVA code这行JAVA代码
mViewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class);
had to be changed to必须改为
mViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
and then it all worked for me.然后这一切都对我有用。
Based on: An outline of the steps that created the problem for me基于:对我造成问题的步骤概述
ViewModelProviders.of() has been deprecated. ViewModelProviders.of() 已被弃用。
Use ViewModelProvider constructors directly as they now handle the default ViewModelProvider.Factory role.直接使用 ViewModelProvider 构造函数,因为它们现在处理默认的 ViewModelProvider.Factory 角色。
Java爪哇
mainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
Kotlin科特林
mainActivityViewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)
Instead of ViewModelProviders
we should now use ViewModelProvider
constructors and it has three:我们现在应该使用ViewModelProvider
构造函数而不是ViewModelProviders
,它具有三个:
public ViewModelProvider(ViewModelStoreOwner owner)
public ViewModelProvider(ViewModelStoreOwner owner, Factory factory)
public ViewModelProvider(ViewModelStore store, Factory factory)
1. If you are not using a ViewModelProvider.Factory
to pass additional arguments to your ViewModel
, you can use the first one. 1.如果您不使用ViewModelProvider.Factory
将其他参数传递给您的ViewModel
,您可以使用第一个。 so:所以:
viewModel = ViewModelProviders.of(this).get(YourViewModel.class);
can be replaced with:可以替换为:
viewModel = new ViewModelProvider(this).get(YourViewModel.class);
AppCompatActivity
and different kinds of Fragment
s are indirect subclasses of ViewModelStoreOwner
(see the complete list of its known subclasses here ), so you can use them in this constructor. AppCompatActivity
和不同种类的Fragment
是ViewModelStoreOwner
的间接子类( 在此处查看其已知子类的完整列表),因此您可以在此构造函数中使用它们。
2. But if you are using a ViewModelProvider.Factory
, you should use the second or the third constructors: 2.但是如果你使用ViewModelProvider.Factory
,你应该使用第二个或第三个构造函数:
viewModel = ViewModelProviders.of(this, viewModelFactory).get(YourViewModel.class);
can be replaced with:可以替换为:
viewModel = new ViewModelProvider(this, viewModelFactory).get(YouViewModel.class);
OR based on the documentation of ViewModelStore
:或基于ViewModelStore
的文档:
Use ViewModelStoreOwner.getViewModelStore() to retrieve a ViewModelStore for activities and fragments.使用 ViewModelStoreOwner.getViewModelStore() 为活动和片段检索 ViewModelStore。
viewModel = new ViewModelProvider(getViewModelStore(), viewModelFactory).get(YourViewModel.class);
ViewModelProviders.of() has been deprecated. ViewModelProviders.of() 已被弃用。 You can pass a Fragment or FragmentActivity to the new ViewModelProvider(ViewModelStoreOwner) constructor to achieve the same functionality.您可以将 Fragment 或 FragmentActivity 传递给新的 ViewModelProvider(ViewModelStoreOwner) 构造函数以实现相同的功能。 (aosp/1009889) (aosp/1009889)
If you're using Kotlin, instead of:如果您使用的是 Kotlin,而不是:
private lateinit var viewModel: EntityGridViewModel
[...]
// Deprecated
viewModel = ViewModelProviders.of(this).get(EntityGridViewModel::class.java)
You can use the nicer:你可以使用更好的:
private val viewModel: EntityGridViewModel by viewModels()
The simple option for the next several months is to stick with stable or beta versions.接下来几个月的简单选择是坚持使用稳定版或测试版。 ViewModelProviders
is only deprecated starting with 2.2.0
, presently in an alpha03
release. ViewModelProviders
仅从2.2.0
开始被弃用,目前在alpha03
版本中。
For when you do move to 2.2.0
or higher of the lifecycle dependencies, your options depend on your language:对于当您迁移到2.2.0
或更高版本的生命周期依赖项时,您的选项取决于您的语言:
If you are using Java, use the ViewModelProvider()
constructor, passing in your activity or fragment如果您使用 Java,请使用ViewModelProvider()
构造函数,传入您的活动或片段
If you are using Kotlin, there is supposed to be a by viewModels()
property delegate, though I am not finding it in the source code...如果您使用的是 Kotlin,则应该有一个by viewModels()
属性委托,尽管我在源代码中没有找到它...
For java
:对于java
:
Simply replace this:简单地替换这个:
viewModel = ViewModelProviders.of(this).get(YOUR_VIEW_MODEL::class.java)
With this:有了这个:
viewModel = ViewModelProvider(this).get(YOUR_VIEW_MODEL::class.java)
if you are having your own factory then,如果你有自己的工厂,那么,
viewModel =
ViewModelProvider(this,YOUR_FACTORY).get(YOUR_VIEW_MODEL::class.java)
For kotlin
: There is a nicer way to do this.对于kotlin
:有一种更好的方法可以做到这一点。 you can add the below dependencies in app build.gradle
:您可以在 app build.gradle
中添加以下依赖项:
//for activity
implementation "androidx.activity:activity-ktx:1.3.0-alpha06"
//for fragment
implementation "androidx.fragment:fragment-ktx:1.3.2"
and then access the viewmodel like below:然后访问视图模型,如下所示:
private val viewmodel: YOUR_VIEW_MODEL by viewModels()
If you have your own view model factory then,如果你有自己的视图模型工厂,
private val viewmodel: YOUR_VIEW_MODEL by viewModels { YOUR_VIEW_MODEL_FACTORY }
This code works for me这段代码对我有用
private lateinit var viewModel: MainViewModel
viewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(MainViewModel::class.java)
you can add the below dependencies in app build.gradle
:您可以在 app build.gradle
中添加以下依赖项:
//for activity
implementation "androidx.activity:activity-ktx:1.2.2"
//for fragment
implementation "androidx.fragment:fragment-ktx:1.3.2"
and then use like below:然后像下面这样使用:
private val viewmodel: YOUR_VIEW_MODEL_CLASS_NAME by viewModels()
If you have your own view model factory then,如果你有自己的视图模型工厂,
private val viewmodel: YOUR_VIEW_MODEL_CLASS_NAME by viewModels { YOUR_VIEW_MODEL_FACTORY }
private lateinit var vm: SRViewModel
private lateinit var adapter: StudentRecordsAdapter
Deprecated已弃用
vm = ViewModelProviders.of(this)[SRViewModel::class.java]
Use this line of code使用这行代码
vm= ViewModelProvider(this)[SRViewModel::class.java]
lifecycle-extensions 2.2.0 version:生命周期扩展 2.2.0 版本:
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
ViewModelProvider with constructor uses带有构造函数的 ViewModelProvider 使用
// With ViewModelFactory
val viewModel = ViewModelProvider(this, YourViewModelFactory).get(YourViewModel::class.java)
//Without ViewModelFactory
val viewModel = ViewModelProvider(this).get(YourViewModel::class.java)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.