問題描述
查看 ViewModel
的 Google 文檔,他們展示了以下關(guān)于如何獲取 ViewModel
的示例代碼:
val model = ViewModelProviders.of(this).get(MyViewModel::class.java)
當(dāng)使用最新的依賴android.arch.lifecycle:extensions:1.1.1
時沒有ViewModelProviders
這個類.
轉(zhuǎn)到 ViewModelProviders
,我看到一個評論說:
此類在 API 級別 1.1.0 中已棄用.使用 ViewModelProvider.AndroidViewModelFactory
問題是,當(dāng)嘗試使用ViewModelProvider.AndroidViewModelFactory
時,找不到等效的of
方法來獲取ViewModel
的實例.
我嘗試做什么:
ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(PlayerViewHolder::class.java)
因此方法名為 create
,我每次調(diào)用它時都會得到一個新的 ViewModel 實例,這不是我想要的.
有什么想法可以替換上面不推薦使用的代碼嗎?
UPDATE 2020-06-16:目前 ViewModelProviders
已棄用,不應(yīng)再使用.這個問題和答案來自 2018 年底,當(dāng)時情況并非如此.此問題和答案也適用于 ViewModelProviders
的舊架構(gòu)組件版本,而不是 AndroidX 版本.
當(dāng)使用最新的依賴android.arch.lifecycle:extensions:1.1.1
時沒有ViewModelProviders
這個類.
是的,有.為了證明這一點:
在 Android Studio 3.2.1 中創(chuàng)建一個新項目(使用 Kotlin,
minSdkVersion
21,空活動"模板)在
app
模塊的依賴中添加android.arch.lifecycle:extensions:1.1.1
這會給你一個 app/build.gradle
像:
應(yīng)用插件:'com.android.application'應(yīng)用插件:'kotlin-android'應(yīng)用插件:'kotlin-android-extensions'安卓 {compileSdkVersion 28默認配置 {應(yīng)用程序IDcom.commonsware.myandroidarch"minSdkVersion 21targetSdkVersion 28版本代碼 1版本名稱1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}構(gòu)建類型 {發(fā)布 {縮小啟用假proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}}依賴{實現(xiàn)文件樹(目錄:'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'實現(xiàn) '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'}
然后您將看到該庫與該類一起顯示在外部庫"中:
您將能夠引用該類:
包 com.commonsware.myandroidarch導(dǎo)入 android.arch.lifecycle.ViewModelProviders導(dǎo)入 android.support.v7.app.AppCompatActivity導(dǎo)入 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:
This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory
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.
When using the latest dependency
android.arch.lifecycle:extensions:1.1.1
there is no such classViewModelProviders
.
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)
}
}
Going to the documentation for ViewModelProviders, I saw a comment saying: This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory
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?
Use ViewModelProviders
.
這篇關(guān)于ViewModelProviders 在 1.1.0 中已棄用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!