ViewModelProviders is deprecated in 1.1.0

后端 未结 22 1414
说谎
说谎 2020-12-07 09:14

Looking at the Google docs for ViewModel, they show the below sample code on how to get a ViewModel:

val model = ViewModelProviders         


        
相关标签:
22条回答
  • 2020-12-07 09:52

    Import

    Deprecated From:

    import androidx.lifecycle.ViewModelProviders;

    To:

    import androidx.lifecycle.ViewModelProvider;
    

    Using

    Deprecated From:

    ViewModelProviders.of(this, provider).get(VM::class.java)

    To:

    ViewModelProvider(this, provider).get(VM::class.java)
    
    0 讨论(0)
  • 2020-12-07 09:52

    Probably you can just use:

    val viewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(MyViewModel::class.java)
    

    without needing to add android.arch.lifecycle:extensions:1.1.1 as the dependency.

    0 讨论(0)
  • 2020-12-07 09:52

    When you use dagger then u'll pass factory in constructor

      MobileViewModel mobileViewModel = new ViewModelProvider(this,providerFactory).get(MobileViewModel.class);
    
    0 讨论(0)
  • 2020-12-07 09:53

    Try this...

     LocationViewModel locationViewModel = new ViewModelProvider(this).get(LocationViewModel.class);
    
    0 讨论(0)
  • 2020-12-07 09:56

    This answer is for Java but you can understand main point. Solution is that you need use ViewModelProvider because ViewModelProviders deprecated:

    //Do not forget import it.
    import androidx.lifecycle.AndroidViewModel;
    
    ViewModelProvider.AndroidViewModelFactory(getApplication()).create(YourViewModel.class);
    

    Example usage is:

    YourViewModel yourViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(YourViewModel.class);
    

    Also, do not forget update Gradle Dependencies: Check here for last versions of them

    implementation 'androidx.lifecycle:lifecycle-viewmodel:2.2.0'
    annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0'
    

    !!! However, be careful because some answers recommend this solution but it did not work for me:

    yourViewModel = new ViewModelProvider(this).get(YourViewModel.class);
    

    Because when I used this way, I got below Error Message:

    Caused by: java.lang.RuntimeException: Cannot create an instance of class com.canerkaseler.mvvmexample.ViewModel

    @canerkaseler

    0 讨论(0)
  • 2020-12-07 09:58

    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 class ViewModelProviders.

    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 the app 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:

    External Libraries

    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:

    Documentation Screenshot

    Any ideas what is the replacement of deprecated code above?

    Use ViewModelProviders.

    0 讨论(0)
提交回复
热议问题