Search code examples
androidandroid-fragmentsviewmodel

ViewModelProviders.of(getActivity()) "Cannot resolve method of(android.app.Activity)"


I'm currently trying to share data between my two fragments with the help of a view model Android ViewModels.

In the this article they use "ViewModelProviders.of(getActivity()).get(SharedViewModel.class);" in the Fragment to get the model.

My Problem is, that when I write the exact same line into my fragment, I get a "Cannot resolve method of(android.app.Activity)" error

model = ViewModelProviders.of(getActivity()).get(UserAccountViewModel.class);

Can some one help me with this ?


Solution

  • ViewModelProviders requires that you use FragmentActivity (or something derived from it), not Activity, as the base class of your activities.