After rotation, onCreate () Fragment is called before onCreate () FragmentActivity

I use FragmentActivity and Fragments.

When starting the application:

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

Everything is in order, FragmentActivity onCreate () is called before the onCreate () fragment. And when I spin:

 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() 

The onCreate () fragment is called before FragmentActivity onCreate (). Why is this contradictory?

In FragmentActivity onCreate (), I generate some data that Fragment onCreate () receives. Due to this strange behavior, I had to move my code from Fragment onCreate () to Fragment onCreateView () to be sure that my data was generated earlier.

I use FragmentStatePagerAdapter to store fragments, maybe this is the reason?

+47
android oncreate android-fragments android-fragmentactivity android-support-library
Dec 30 '12 at 18:36
source share
2 answers

You should not expect a valid action until the call to onActivityCreated() in the fragment life cycle.

Called when a fragment operation has been created and this fragment view hierarchy has been created. It can be used to perform the final initialization after these parts are in place, for example, to obtain representations or restore state.

The exact reasons why the restructuring order is not linear, I cannot tell you. It is probably more efficient to let each component resume its own pace, rather than force a tough order. For example, I prefer my LoaderManager to start as early as possible, and we will worry about the layout for it later.

(I like a good chart.)

enter image description here

+55
Dec 30 '12 at 18:42
source share

Fragments are restored during Activity onCreate() . It is important to note that they are restored in the base class Activity onCreate() . Thus, if you first call super.onCreate() , then the rest of your onCreate() method will be executed after the fragments are restored.

One possible solution is to restore your state or calculate what data your fragment will need BEFORE you call super.onCreate()

The life cycle is as follows:

 ACTIVITY onCreate (pre-super) FRAGMENT onAttach ACTIVITY onCreate (post-super) 

So do something like this:

 @Override public void onCreate( final Bundle savedInstanceState ) { Log.d( TAG, "ACTIVITY onCreate (pre-super)" ); // Do your processing here super.onCreate( savedInstanceState ); // Fragments will be restored here Log.d( TAG, "ACTIVITY onCreate (post-super)" ); } 
+25
May 13 '15 at 18:45
source share



All Articles