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

AndroidAndroid FragmentsOncreateAndroid FragmentactivityAndroid Support-Library

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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][1]

[1]: http://i.stack.imgur.com/PqYDU.png "Diagram!"

Solution 2 - Android

Fragments are restored during the Activity's onCreate(). Importantly though, they are restored in the base Activity class's onCreate(). Thus if you call super.onCreate() first, all of the rest of your onCreate() method will execute after your Fragments have been restored.

One possible solution then is to restore your state or calculate what ever data it is your Fragment's will need BEFORE you call super.onCreate()

The life cycle looks like this:

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)" );
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAppiDevoView Question on Stackoverflow
Solution 1 - AndroidSamView Answer on Stackoverflow
Solution 2 - AndroidAdamView Answer on Stackoverflow