Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred Fragment lifecycle method for instantiating ViewModels

In the android-architecture-components/GithubBrowserSample repo, the Fragment#onViewCreated lifecycle method is being used for ViewModel instantiation (with the Fragment's scope) for the Fragments that use the combination of databinding + LiveData + ViewModel:

From SearchFragment.kt of that repo ^:

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        searchViewModel = ViewModelProviders.of(this, viewModelFactory)
        ...
    }

Is there any official guideline or consensus as to which of these Fragment lifecycle methods such as onAttach, onCreate, onViewCreated, or onActivityCreated is the best/safest place to instantiate the Fragment's ViewModel using the ViewModelProviders.of(fragment, viewModelFactory) method? (given the databinding + LiveData combo, if that makes a difference)

Trying to make sense of the general advantages/disadvantages of putting ViewModel instantiation into any of the early lifecycle methods such as onAttach/onCreate, for example (after calling super, of course).

Thanks in advance.

like image 924
nebulasmoothie Avatar asked Feb 26 '19 09:02

nebulasmoothie


People also ask

Can you initiate ViewModel directly without Viewmodelprovider?

android - Instantiate ViewModels directly, without making use of ViewModelProviders.

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

Can a fragment have a ViewModel?

Use the ViewModel to update the UI That means the view model can be shared across fragments. Each fragment could access the view model to check on some detail of the order or update some data in the view model.

Which lifecycle method is being used for ViewModel instantiation?

In the android-architecture-components/GithubBrowserSample repo, the Fragment#onViewCreated lifecycle method is being used for ViewModel instantiation (with the Fragment 's scope) for the Fragment s that use the combination of databinding + LiveData + ViewModel:

What is the lifecycle of a view fragment?

These include onCreate () , onStart () , onResume () , onPause () , onStop (), and onDestroy (). A fragment's view has a separate Lifecycle that is managed independently from that of the fragment's Lifecycle.

How to create a ViewModel from a fragment?

Use activity's scope to create an instance of the ViewModel in fragment so that all the fragments and activity now has a common ViewModel and have access to the same ViewModelStore. If the activity is re-created, it and all its fragments receive the same ViewModel instance that was created by the associated activity.

How do I get the lifecycleowner of a fragment?

A fragment's view has a separate Lifecycle that is managed independently from that of the fragment's Lifecycle. Fragments maintain a LifecycleOwner for their view, which can be accessed using getViewLifecycleOwner () or getViewLifecycleOwnerLiveData () .


2 Answers

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    searchViewModel = ViewModelProvider().get(this, viewModelFactory)

This is correct, the common mistake tends to be the lifecycle owner used to observe the LiveData.

    // also in onViewCreated
    searchViewModel.observe(viewLifecycleOwner) { items ->
        ....
    }
like image 87
EpicPandaForce Avatar answered Nov 15 '22 18:11

EpicPandaForce


There is no direct advantage of one to another as far as I know, since #onViewCreated is immediately being called after #onCreateView finishes. According to the docs:

void onViewCreated (View view, Bundle savedInstanceState)

Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.

I normally prefer putting all initializations (if not related to my view-hierarchy) into #onViewCreated method. It has never been a problem in my case.

like image 35
Onur D. Avatar answered Nov 15 '22 19:11

Onur D.