Home » Android » android – ViewModel cannot be instantiated in an Activity

android – ViewModel cannot be instantiated in an Activity

Posted by: admin June 15, 2020 Leave a comment


I am trying to instantiate one ViewModel to use across all of my Activity(s).

public class LaunchActivity extends Activity {
    private Controller control;

    protected void onCreate(Bundle savedInstanceState) {
         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?

How to&Answers:

You need to use the support library activity.

AppCompatActivity or FragmentActivity


@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.


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.


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):

private ViewModelStore viewModelStore = null;

public Object onRetainNonConfigurationInstance() {
    return viewModelStore;

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 =
    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.


I am using this:

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