简体   繁体   English

由于不推荐使用 ViewModelProviders.of(),我应该如何创建 ViewModel 的 object?

[英]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和不同种类的FragmentViewModelStoreOwner的间接子类( 在此处查看其已知子类的完整列表),因此您可以在此构造函数中使用它们。

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)

Please click here to see the solution 请点击这里查看解决方案

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)

This class is deprecated.此类已弃用。 Use the constructors for ViewModelProvider directly.直接使用 ViewModelProvider 的构造函数。 here 这里

So instead of using this所以而不是使用这个

ViewModelProviders.of(this).get(MyViewModel.class); - deprecated

Use this one用这个

new ViewModelProvider(this).get(MyViewModel.class); - correct

As ViewModelProviders got deprecated.由于ViewModelProviders已被弃用。 You can now use the ViewModelProvider constructor directly.您现在可以直接使用ViewModelProvider构造函数。 For more details how to use it, check here .有关如何使用它的更多详细信息,请查看此处

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.

相关问题 当 ViewModelProviders.of() 被弃用时,如何在 Activity 和 Service 之间共享相同的 ViewModel? - How to share the same ViewModel between Activity and Service when ViewModelProviders.of() is deprecated? 如何在Non-FragmentActivity上使用ViewModelProviders.of()获取ViewModel? - How to fetch the ViewModel using ViewModelProviders.of() on non-FragmentActivity? 我无法在 ViewModelProviders.of() function 中将片段作为参数 - I can't give fragment as argument inside ViewModelProviders.of() function ViewModelProviders.of(FragmentActivity).get(ViewModel :: class.java)要求活动而不是get()调用中的viewmodel - ViewModelProviders.of(FragmentActivity).get(ViewModel::class.java) asking for activity instead of viewmodel in get() call 如何在没有ViewModelProviders类的情况下获取ViewModel? - How do I get a ViewModel without ViewModelProviders class? ViewModelProviders.of(activity) 只接受片段或片段活动 - ViewModelProviders.of(activity) only accept fragment or fragmentactivity Kotlin 不理解 ViewModelProviders.of(activity ?: fragment) - Kotlin does not understand ViewModelProviders.of(activity ?: fragment) 如何在 JAVA 中弃用 ViewModelProviders 后实现 ViewModelProvider - How to implement ViewModelProvider after ViewModelProviders deprecated in JAVA 为什么我应该为视图模型使用 viewmodelproviders? - Why should I use viewmodelproviders for viewmodels? ViewModelProviders 在 1.1.0 中已弃用 - ViewModelProviders is deprecated in 1.1.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM