ViewModelProviders is deprecated in 1.1.0

后端 未结 22 1440
说谎
说谎 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:15

    As @FantasyFang mentioned in his answer, use the lastest version for the lifecycle:lifecycle-extensions which in this moment is 2.2.0-alpha03. So you should add in your build.gradle file the following line:

    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha03' 
    

    For those who are using Java, to solve this, pass those arguments directly to ViewModelProvider's constructor:

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

    Or if you don't use a factory, simply use:

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

    Without passing your the factory object.

提交回复
热议问题