Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve ViewModelProvider construction in a fragment?

I have been spending a lot of time trying to figure out why in the code below (towards the end), I get an error on ViewModelProvider(this). I also tried getActivity() instead of 'this', same issue. The error I get is "Cannot resolve constructor ..."


import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;



public class ItemSetupFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setup, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ItemSetupFragmentModel model = new ViewModelProvider(this).get(ItemSetupFragmentModel.class);
        model.getKids().observe(this, users -> {
            // update UI
        });
    }
}

Image showing compilation error in Android Studio

like image 644
LeaningAndroid Avatar asked Feb 05 '20 06:02

LeaningAndroid


People also ask

What is viewmodelprovider in Android Studio fragment?

ViewModelProvider of (Fragment fragment) Creates a ViewModelProvider, which retains ViewModels while a scope of given fragment is alive. More detailed explanation is in ViewModel. It uses ViewModelProvider.AndroidViewModelFactory to instantiate new ViewModels.

What is a viewmodelprovider?

A ViewModel that is an instance of the given type T . Returns an existing ViewModel or creates a new one in the scope (usually, a fragment or an activity), associated with this ViewModelProvider .

Why is my viewmodelprovider constructor not working?

You aren't using the latest library release in which the ViewModelProvider (@NonNull ViewModelStoreOwner owner) constructor was included. You are seeing the latest docs but not using the latest library version of ViewModel. You need to use The cleanest implementation must use Kotlin for its advanced functions.

What is the scope of the created ViewModel?

The created ViewModel is associated with the given scope and will be retained as long as the scope is alive (e.g. if it is an activity, until it is finished or process is killed). String: The key to use to identify the ViewModel. Class: The class of the ViewModel to create an instance of it if it is not present.


Video Answer


2 Answers

Firstly you need to use the latest version of lifecycle extension. It should be:

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0" 

or any updated version.

Then you should use requireActivity() instead of getActivity(). This way you will ensure that the activity is attached an not getting a NullPointerException.

ItemSetupFragmentModel model = new ViewModelProvider(requireActivity()).get(ItemSetupFragmentModel.class);

Note: ViewModel Overview and Declaring Dependencies

I had to restart cache after adding the library to the Gradle file. There is no need to use requireActivity(), this is enough.

like image 137
Shababb Karim Avatar answered Sep 19 '22 03:09

Shababb Karim


You aren't using the latest library release in which the ViewModelProvider(@NonNull ViewModelStoreOwner owner) constructor was included. You are seeing the latest docs but not using the latest library version of ViewModel. You need to use

    implementation 'androidx.lifecycle:lifecycle-viewmodel:2.2.0' // For Java

or

    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' // For kotlin extension
like image 23
Somesh Kumar Avatar answered Sep 21 '22 03:09

Somesh Kumar