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
:
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.
android - Instantiate ViewModels directly, without making use of ViewModelProviders.
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.
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.
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:
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.
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.
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 () .
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 ->
....
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With