Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragments recreated on orientation change

I'm developing an app that basically has an ActionBar. When my app starts, the Activity creates the fragments and attaches them to each tab, so when I switch I get different views.

The problems arise when I try to rotate the device. After some struggle, I noticed that Android automatically recreates the previously added fragments like this:

SummaryFragment.onCreate(Bundle) line: 79   
FragmentManagerImpl.moveToState(Fragment, int, int, int) line: 795  
FragmentManagerImpl.moveToState(int, int, int, boolean) line: 1032  
FragmentManagerImpl.moveToState(int, boolean) line: 1014    
FragmentManagerImpl.dispatchCreate() line: 1761 
DashboardActivity(Activity).onCreate(Bundle) line: 864  
...

and then I recreate the fragments as usual. So I have the "real" fragments that I expect to work correctly and their "hidden" Android-created counterparts that make my app crash. How can I avoid this behavior? I already tried to call setRetainInstance(false) in the SummaryFragment.

Thank you

like image 346
Luke47 Avatar asked May 17 '12 11:05

Luke47


People also ask

What happens if I change the orientation of an activity fragment?

If you allow Android to handle orientation changes, it will take care of re-instantiating your fragments, re-adding them to the activity, and recreating the fragment backstack, when it recreates the parent Activity.

What happens when orientation changes in Android apps?

Sometimes handling the orientation changes for your Activity, Fragment or AsyncTasks becomes most frustrating things to deal. If orientation changes is not handle properly then it results unexpected behavior of the application. When such changes occurs, Android restarts the running Activity means it destroy and again created.

Why does Android destroy an existing activity or fragment?

Why? When configurations changed during run time (such as screen orientation, keyboard availability, and language), Android usually destroys application’s existing Activity or Fragment and recreate it. Android does this so that ap p lication can reload its resources based on the new configuration.

Why does Android not call oncreate and ondestroy after orientation change?

Notice that Android does not call onCreate and onDestroy because we retained the Fragment; nor does it call the constructor, because the same Fragment instance will be used after the orientation change.


1 Answers

You need to check for a savedInstanceState [edit: in your parent activity], and if it exists, don't create your fragments.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState == null) {
         // Do your oncreate stuff because there is no bundle   
    }
// Do stuff that needs to be done even if there is a saved instance, or do nothing
}
like image 64
Barak Avatar answered Nov 16 '22 02:11

Barak