问题描述
查看 ViewModel
的 Google 文档,他们展示了关于如何获取ViewModel
的以下示例代码:
val model = ViewModelProviders.of(this).get(MyViewModel::class.java)
当使用最新的依赖android.arch.lifecycle:extensions:1.1.1
时,没有这样的类ViewModelProviders
.
转到ViewModelProviders
文档>,我看到一条评论说:
该类在 API 级别 1.1.0 中已弃用.使用 ViewModelProvider.AndroidViewModelFactory
问题是,当尝试使用 ViewModelProvider.AndroidViewModelFactory
时,找不到等效的 of
方法来获取 ViewModel
的实例.
我尝试做的事情:
ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(PlayerViewHolder::class.java)
因此有了 create
方法的名称,我每次调用它时都会得到一个新的 ViewModel 实例,这不是我想要的.
任何想法替换上面已弃用的代码是什么?
UPDATE 2020-06-16:目前 ViewModelProviders
已弃用并且不应再使用.这个问题和答案来自 2018 年底,当时情况并非如此.此问题和答案也适用于 ViewModelProviders
的旧架构组件版本,而不是 AndroidX 版本.
当使用最新的依赖android.arch.lifecycle:extensions:1.1.1
时,没有这样的类ViewModelProviders
.
是的,有.为了证明这一点:
在 Android Studio 3.2.1 中创建一个新项目(使用 Kotlin,
minSdkVersion
21,空活动"模板)在
app
模块的依赖中添加android.arch.lifecycle:extensions:1.1.1
这会给你一个 app/build.gradle
像:
应用插件:'com.android.application'应用插件:'kotlin-android'应用插件:'kotlin-android-extensions'安卓 {compileSdkVersion 28默认配置{applicationId "com.commonsware.myandroidarch"minSdk 版本 21目标SDK版本28版本代码 1版本名称1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}构建类型{释放 {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}}依赖{实现文件树(目录:'libs',包括:['*.jar'])实现"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"实现 'com.android.support:appcompat-v7:28.0.0'实现 'com.android.support.constraint:constraint-layout:1.1.3'实现 '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'}
然后,您将看到该库与该类一起显示在外部库"中:
并且您将能够引用该类:
package com.commonsware.myandroidarch导入 android.arch.lifecycle.ViewModelProviders导入 android.support.v7.app.AppCompatActivity导入 android.os.Bundle类 MainActivity : AppCompatActivity() {覆盖 fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val provider = ViewModelProviders.of(this)}}
转到 ViewModelProviders 的文档,我看到一条评论说:该类在 API 级别 1.1.0 中已弃用.使用 ViewModelProvider.AndroidViewModelFactory
该注释位于 ViewModelProviders.DefaultFactory
类条目下方并引用该类,而不是 ViewModelProviders
:
任何想法替换上面已弃用的代码是什么?
使用ViewModelProviders
.
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)
When using the latest dependency android.arch.lifecycle:extensions:1.1.1
there is no such class ViewModelProviders
.
Going to the documentation for ViewModelProviders
, I saw a comment saying:
The problem is, when trying to use ViewModelProvider.AndroidViewModelFactory
, cannot find an equivalent of
method to get the instance of the ViewModel
.
What i tried doing:
ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(PlayerViewHolder::class.java)
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: 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.
Yes, there is. To demonstrate this:
Create a new project in Android Studio 3.2.1 (with Kotlin,
minSdkVersion
21, "empty activity" template)Add
android.arch.lifecycle:extensions:1.1.1
to the dependencies of theapp
module
This will give you an app/build.gradle
like:
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:
And you will be able to reference 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)
}
}
That comment is underneath the ViewModelProviders.DefaultFactory
class entry and refers to that class, not ViewModelProviders
:
Use ViewModelProviders
.
这篇关于ViewModelProviders 在 1.1.0 中被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!