ViewModelProviders is deprecated in 1.1.0

后端 未结 22 1433
说谎
说谎 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 10:14

    As of 2.2.0. the lifecycle-extensions has been deprecated. Refer to Google Documentation.

    This is the cut from the page:

    The APIs in lifecycle-extensions have been deprecated. Instead, add dependencies for the specific Lifecycle artifacts you need.

    The new libraries are:

    // ViewModel and lifecycle support for java
    implementation "androidx.lifecycle:lifecycle-viewmodel:${versions.lifecycle}"
    implementation "androidx.lifecycle:lifecycle-livedata:${versions.lifecycle}"
    
    // ViewModel and lifecycle support for kotlin
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.lifecycle}"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:${versions.lifecycle}"
    

    The new code for JAVA:

    viewModel = new ViewModelProvider(this).get(MyViewModel.class);
    

    Or for Kotlin:

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

提交回复
热议问题