Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is onActivityCreated() called after onViewCreated() in a Fragment?

I have two ViewModels. One is used from the Fragment only and the other one is a shared ViewModel from the Activity.

Fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    viewModel = ViewModelProviders.of(this).get(FragmentViewModel.class);
    ...
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    activityViewModel = ViewModelProviders.of(getActivity()).get(ActivityViewModel.class);
}

But in order to know if I can use the content from the activity's ViewModel, I need to know if the onActivityCreated(...) is called after onViewCreated(...) so I can request my data in the Fragment ViewModel based on data I have in the Activity's ViewModel.

To summarise:

Is it for certain that onActivityCreated(...) is called after onViewCreated(...) has finished?

like image 473
Javatar Avatar asked May 22 '19 15:05

Javatar


People also ask

What is onActivityCreated in fragment?

onActivityCreated() is a callback provided to Fragments ostensibly for when the activity is finished being created, but it ends up actually being tied into the Fragment view's lifecycle - it is actually called between onViewCreated() and onViewStateRestored() and can be called multiple times in cases where the Fragment ...

What are the differences between onCreate () onCreateView () and onActivityCreated () in fragments and what would they each be used for?

onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.

What is onViewCreated in fragment?

onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .


Video Answer


2 Answers

After some further research I think I found the answer.

onActivityCreated added in version 22.1.0 void onActivityCreated (Bundle savedInstanceState)

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).

Based on the documentation:

..fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place..

The view hierarchy should be fully instantiated and therefore onActivityCreated will be called after the completion of onViewCreated

UPDATE

Info: onActivityCreated is now deprecated

use onViewCreated(View, Bundle) for code touching the Fragment's view and onCreate(Bundle) for other initialization. To get a callback specifically when a Fragment activity's Activity.onCreate(Bundle) is called, register a androidx.lifecycle.LifecycleObserver on the Activity's Lifecycle in onAttach(Context), removing it when it receives the Lifecycle.State.CREATED callback.

like image 136
Javatar Avatar answered Oct 25 '22 01:10

Javatar


Yes, it is for certain, as you found out. Also, see this answer for a cute little lifecycle drawing (the second one).

like image 33
Ricardo Costeira Avatar answered Oct 25 '22 01:10

Ricardo Costeira