I just want to get a reference in my Fragment (extends Fragment) to my ViewModel.class (extends from AndroidViewModel). This is how it is described everywhere:
UserModel userModel = ViewModelProviders.of(getActivity()).get(UserModel.class);
description on Android Developer
..., but ViewModelProviders is deprecated since long ago.
*This class was deprecated in API level 1.1.0. *
I can't import the class with: android.arch.lifecycle.[ViewModelProviders]
it just offers ViewModelProvider and other stuff.
How am I able to get an instance of my UserModel.class?
ViewModelProviders
is not deprecated. So, useViewModelProviders
:There are pieces of
ViewModelProviders
that are deprecated, such as its constructor, but the class overall is not.Here's how to write a ViewModel
And then to use it in your actctivity do something like this