Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

As ViewModelProviders.of() is deprecated, how should I create object of ViewModel?

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.

like image 935
Anchal Arora Avatar asked Aug 17 '19 08:08

Anchal Arora


People also ask

What can I use instead of Viewmodelproviders?

The android. arch Architecture Components packages are no longer maintained. They have been superseded by the corresponding androidx.

Is Viewmodelproviders deprecated?

This class is deprecated.

What happens to ViewModel when activity is destroyed?

E.g. if it is an Activity, until it is finished. In other words, this means that a ViewModel will not be destroyed if its owner is destroyed for a configuration change (e.g. rotation). The new instance of the owner will just re-connected to the existing ViewModel.


Video Answer


4 Answers

Simply replace:

This:

boardViewModel = ViewModelProviders.of(this).get(BoardViewModel::class.java)

With this:

boardViewModel = ViewModelProvider(this).get(BoardViewModel::class.java)
like image 197
KnowIT Avatar answered Oct 18 '22 06:10

KnowIT


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 enter image description here

like image 30
Denis Avatar answered Oct 18 '22 06:10

Denis


ViewModelProviders.of() has been deprecated. enter image description here

Use ViewModelProvider constructors directly as they now handle the default ViewModelProvider.Factory role.

Java

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

Kotlin

mainActivityViewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)
like image 35
Junaid Avatar answered Oct 18 '22 05:10

Junaid


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);
like image 14
Amin Dannak Avatar answered Oct 18 '22 06:10

Amin Dannak