I had the same issue and I fixed it adding this to the Navigation Drawer activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null){
//Handle the initial fragment transaction
}
...
}
For example, I have a navigation drawer with "Home", "Settings" and "About" as menu items, each with a fragment "home_fragment", "settings_fragment" and "about_fragment".
If I want "home_fragment" to appear when the navigation drawer activity launches, I use this code on the OnCreate function:
FragmentManager fM = getSupportFragmentManager();
fM.beginTransaction().replace(R.id.NavDrawContent,new home_fragment()).commit();
But I want it to execute only when (savedInstanceState == null), so that when we change the phone orientation while in settings_fragment (for example), it doesn't inflate home_fragment.
So the final code (inside the navigation drawer activity OnCreate):
super.onCreate(savedInstanceState);
if(savedInstanceState==null){
FragmentManager fM = getSupportFragmentManager();
fM.beginTransaction().replace(R.id.NavDrawContent,new home_fragment()).commit();
}
I found many post explaining how get the Fragment from the savedInstanceState Bundle
- really? In an Activity you can callisChangingConfigurations()
method AFTERonPause()
to find out if a configuration change is occurring or not. Not exactly what you're after, but this will return true if a configuration change is happening, and false if not i.e. Activity is being destroyed and not recreated. – OvershoeFragmentManager
. As soon as your new activity gets created, it will take care of adding those fragment to this newly created activity. What issue do you have exactly? – Lunchroom