ViewModel cannot be instantiated in an Activity
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.
You need to use the support library
activity.
AppCompatActivity
or FragmentActivity
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.
@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.