ViewModelProviders在1.1.0中已弃用 [英] ViewModelProviders is deprecated in 1.1.0

查看:463
本文介绍了ViewModelProviders在1.1.0中已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 Google文档中的ViewModel,它们显示以下内容有关如何获取ViewModel的示例代码:

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)

使用最新的依赖项android.arch.lifecycle:extensions:1.1.1时,没有此类ViewModelProviders.

When using the latest dependency android.arch.lifecycle:extensions:1.1.1 there is no such class ViewModelProviders.

转到ViewModelProviders文档,我看到了评论说:

Going to the documentation for ViewModelProviders, I saw a comment saying:

该类在API级别1.1.0中已弃用.使用ViewModelProvider.AndroidViewModelFactory

This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory

问题是,当尝试使用ViewModelProvider.AndroidViewModelFactory时,找不到等效的of方法来获取ViewModel的实例.

The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory, cannot find an equivalent of method to get the instance of the ViewModel.

我尝试做的事情:

ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(PlayerViewHolder::class.java)

由于方法create的名称,每次调用它时我都会得到一个ViewModel的新实例,这不是我想要的.

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?

推荐答案

UPDATE 2020-06-16 :当前 ViewModelProviders已被弃用,不应再使用.这个问题和答案来自2018年末,当时情况并非如此.这个问题和答案也适用于ViewModelProviders的较旧的Architecture Components版本,而不是AndroidX版本.

UPDATE 2020-06-16: Presently ViewModelProviders is deprecated and should no longer be used. This question and answer were from late 2018, when that was not the case. This question and answer are also for the older Architecture Components edition of ViewModelProviders, not the AndroidX edition.

使用最新的依赖项android.arch.lifecycle:extensions:1.1.1时,没有此类ViewModelProviders.

是的,有.为了证明这一点:

Yes, there is. To demonstrate this:

  • 在Android Studio 3.2.1中创建一个新项目(使用Kotlin,minSdkVersion 21,空活动"模板)

  • Create a new project in Android Studio 3.2.1 (with Kotlin, minSdkVersion 21, "empty activity" template)

android.arch.lifecycle:extensions:1.1.1添加到app模块的依赖项

这将为您提供app/build.gradle,如:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.commonsware.myandroidarch"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

然后您将看到该库显示在该类的外部库"中:

You will then see that library show up in "External Libraries" with that class:

您将可以引用该类:

package com.commonsware.myandroidarch

import android.arch.lifecycle.ViewModelProviders
import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val provider = ViewModelProviders.of(this)
  }
}

转到ViewModelProviders的文档,我看到一条评论说:该类在API级别1.1.0中已弃用.使用ViewModelProvider.AndroidViewModelFactory

Going to the documentation for ViewModelProviders, I saw a comment saying: This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory

该注释位于ViewModelProviders.DefaultFactory类条目的下面,并且引用该类,而不是ViewModelProviders:

That comment is underneath the ViewModelProviders.DefaultFactory class entry and refers to that class, not ViewModelProviders:

有什么想法替换上面不推荐使用的代码吗?

Any ideas what is the replacement of deprecated code above?

使用ViewModelProviders.

这篇关于ViewModelProviders在1.1.0中已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆