ListFragment and onActivityCreated are called twice

I have a listFragment, this is my onActivityCreated:

@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (savedInstanceState != null) { utenti=savedInstanceState.getParcelableArrayList("array"); }else{ threadutenti= (GetUtenti) new GetUtenti(this.getActivity(), utenti).execute(); } 

When I rotate my device, I first saved the InstanceState, but not null (I got it), but after onActivityCreated is called with onActivityCreated null! What for? I want to get my utenti object on a ListFragment, and not on my activity.

+4
source share
1 answer

This can happen if you always create a new fragment in the Activity#onCreate() .
If true, check this answer . Your Fragment#onActivityCreated will be called twice:

  • First time called by calling FragmentActivity#onStart on mFragments.dispatchActivityCreated() . It will give the saved copy.
  • The second time called by FragmentActivity#onStart calling mFragments.execPendingActions() . This time without a saved instance (since it is called in response to adding a new fragment).
+8
source

All Articles