As ViewModelProviders.of() is deprecated, How should i create object of ViewModel?

前端 未结 10 1071
情深已故
情深已故 2020-12-14 14:05

I have been trying to create an Object of ViewModel in an Activity but ViewModelProviders is deprecated So what\'s the alternative to create the ViewModel\'s object.

相关标签:
10条回答
  • 2020-12-14 14:18

    This class is deprecated. Use the constructors for ViewModelProvider directly. here

    So instead of using this

    ViewModelProviders.of(this).get(MyViewModel.class); - deprecated
    

    Use this one

    new ViewModelProvider(this).get(MyViewModel.class); - correct
    
    0 讨论(0)
  • 2020-12-14 14:23

    This Gradle upgrade created the problem for me.

    FROM

    implementation 'androidx.core:core:1.1.0'
    

    TO

    implementation 'androidx.core:core:1.2.0'
    

    IN MAIN ACTIVITY Java/Kotlin Files

    This import statement

    import androidx.lifecycle.ViewModelProviders
    

    had to be changed to

    import androidx.lifecycle.ViewModelProvider
    

    This KOTLIN viewModel statement

    viewModel = ViewModelProviders.of(this).get(MainActivityViewModel::class.java)
    

    had to be changed to

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

    and in JAVA

    This line of JAVA code

    mViewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class);
    

    had to be changed to

    mViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
    

    and then it all worked for me.

    Based on: An outline of the steps that created the problem for me

    0 讨论(0)
  • 2020-12-14 14:25

    ViewModelProviders.of() has been deprecated. You can pass a Fragment or FragmentActivity to the new ViewModelProvider(ViewModelStoreOwner) constructor to achieve the same functionality. (aosp/1009889)

    Please click here to see the solution

    0 讨论(0)
  • 2020-12-14 14:27

    If you're using Kotlin, instead of:

    private lateinit var viewModel: EntityGridViewModel
    
    [...]
    
    // Deprecated
    viewModel = ViewModelProviders.of(this).get(EntityGridViewModel::class.java)
    

    You can use the nicer:

    private val viewModel: EntityGridViewModel by viewModels()
    
    0 讨论(0)
  • 2020-12-14 14:39

    Instead of ViewModelProviders we should now use ViewModelProvider constructors and it has three:

    public ViewModelProvider(ViewModelStoreOwner owner)
    public ViewModelProvider(ViewModelStoreOwner owner, Factory factory)
    public ViewModelProvider(ViewModelStore store, Factory factory)
    

    1. If you are not using a ViewModelProvider.Factory to pass additional arguments to your ViewModel, you can use the first one. so:

    viewModel = ViewModelProviders.of(this).get(YourViewModel.class);
    

    can be replaced with:

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

    AppCompatActivity and different kinds of Fragments are indirect subclasses of ViewModelStoreOwner (see the complete list of its known subclasses here), so you can use them in this constructor.

    2. But if you are using a ViewModelProvider.Factory, you should use the second or the third constructors:

    viewModel = ViewModelProviders.of(this, viewModelFactory).get(YourViewModel.class);
    

    can be replaced with:

    viewModel = new ViewModelProvider(this, viewModelFactory).get(YouViewModel.class);
    

    OR based on the documentation of ViewModelStore:

    Use ViewModelStoreOwner.getViewModelStore() to retrieve a ViewModelStore for activities and fragments.

    viewModel = new ViewModelProvider(getViewModelStore(), viewModelFactory).get(YourViewModel.class);
    
    0 讨论(0)
  • 2020-12-14 14:41

    As ViewModelProviders got deprecated. You can now use the ViewModelProvider constructor directly. For more details how to use it, check here.

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