Reputation: 592
I am trying to instantiate one ViewModel to use across all of my Activity(s).
public class LaunchActivity extends Activity {
private Controller control;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
control = ViewModelProviders.of(this).get(Controller.class);
}
}
but I got an error at control = ViewModelProviders.of(this) <--
stating that it cannot resolve of(this)
, but based on the example here by Android Developer on Implementing ViewModel; a class that extends ViewModel should be able to be instantiated on Activity, am I right?
If what I am doing is wrong, how should I instantiate a ViewModel object for my Activity(s)? Do I have to create n
number of Activity(s) with Fragment(s) since ViewModelProviders.of()
only works with Fragment?
Upvotes: 10
Views: 13527
Reputation: 368
For me the I was extending AppCompatActivity
, but still it was not working. But implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
was missing, adding which it worked fine.
Upvotes: 1
Reputation: 6302
@elmorabea's Answer was correct, but one important thing to note here is that if you have enabled AndroidX and are still getting an issue indicating that you need to pass in a fragment instead of an activity, make sure that your class extends the correct AppCompatActivity. IE, make sure you are using:
import androidx.appcompat.app.AppCompatActivity;
and not
import android.support.v7.app.AppCompatActivity;
As one is from AndroidX while the other is not. For more info on the AndroidX migrations, see this link.
Upvotes: 10
Reputation: 1887
I am using this:
LoginViewModel viewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
Ref:https://developer.android.com/topic/libraries/architecture/viewmodel#java
Upvotes: 1
Reputation: 1333
Another solution if you don't want to change your activity to extend from another Activity class (in case your BaseActivity is already used and rely heavily on Activity
and maybe cause an error if change the parent activity class):
@Nullable
private ViewModelStore viewModelStore = null;
@Override
public Object onRetainNonConfigurationInstance() {
return viewModelStore;
}
@NonNull
private ViewModelStore getViewModelStore() {
Object nonConfigurationInstance = getLastNonConfigurationInstance();
if (nonConfigurationInstance instanceof ViewModelStore) {
viewModelStore = (ViewModelStore) nonConfigurationInstance;
}
if (viewModelStore == null) {
viewModelStore = new ViewModelStore();
}
return viewModelStore;
}
public ViewModelProvider getViewModelProvider() {
ViewModelProvider.Factory factory =
ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
return new ViewModelProvider(getViewModelStore(), factory);
}
This logic is similar to what is in FragmentActivity
Then, instead of calling ViewModelProviders.of(this)
, we just need to call getViewModelProvider()
. For getting Controller
:
control = getViewModelProvider().get(Controller.class)
By doing this, we don't need to add android.arch.lifecycle:extensions
dependency.
Upvotes: 5
Reputation: 3263
You need to use the support library
activity.
AppCompatActivity
or FragmentActivity
Upvotes: 24
Reputation: 1006674
You are extending Activity
. ViewModelProviders
works with FragmentActivity
and things that inherit from that, such as AppCompatActivity
. It also works with the backport of Fragment
(android.support.v4.app.Fragment
). There is no official support for the native Activity
or Fragment
class.
Upvotes: 5