Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After the rotate, onCreate() Fragment is called before onCreate() FragmentActivity

I'm using FragmentActivity and Fragments.

When the application starts:

FragmentActivity onCreate() <------ FragmentActivity onStart() FragmentActivity onResume() Fragment onAttach() Fragment onCreate() <------ Fragment onCreateView() Fragment onActivityCreated() Fragment onStart() Fragment onResume() 

Everything is OK, FragmentActivity onCreate() is called before Fragment onCreate(). And when I rotate:

Fragment onPause() FragmentActivity onPause() Fragment onStop() FragmentActivity onStop() Fragment onDestroyView() Fragment onDestroy() Fragment onDetach() FragmentActivity onDestroy() --- Fragment onAttach() Fragment onCreate() <---------- FragmentActivity onCreate() <--------- Fragment onCreateView() Fragment onActivityCreated() Fragment onStart() FragmentActivity onStart() FragmentActivity onResume() Fragment onResume() 

Fragment onCreate() is called before FragmentActivity onCreate(). Why is it inconsistent?

In FragmentActivity onCreate() I generate some data, which Fragment onCreate() gets. Because of that strange behaviour I had to move my code from Fragment onCreate() to Fragment onCreateView() to be sure that my data had been generated before.

I'm using FragmentStatePagerAdapter to hold Fragments, maybe that is the reason?

like image 589
AppiDevo Avatar asked Dec 30 '12 18:12

AppiDevo


People also ask

Is onAttach called before onCreate?

In a Fragment's Lifecycle, the onAttach() method is called before the onCreate() method.

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.

Does fragment have onCreate?

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.


1 Answers

You should not count on a valid Activity until the onActivityCreated() call in the Fragment's life cycle.

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.

The exact reasons why the rebuild order is not linear, I cannot tell you. It is probably more efficient to allow each component to re-start at its own pace rather than forcing a rigid order. For instance, I prefer that my LoaderManager starts as early as possible and we'll worry about the layout for it's content later.

(I love a good diagram.)

enter image description here

like image 57
Sam Avatar answered Sep 27 '22 17:09

Sam