Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment stack empty after rotate

Tags:

android

I'm using a FragmentActivity to display a stack of Fragments in a single activity app. This is working fine and I can move through the fragments and hit back to go back up the stack until I rotate the device when I end up with the Fragment on the top of the stack rotated but hitting the back button takes me back to the root Fragment with all other Fragments in between gone.

I'm using the following code to add a Fragment to the stack:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.mainContentViewContainer, newFragment, "DetailFragment");
transaction.addToBackStack(null);
transaction.commit();

What could be causing the stack to be empty after rotate?

like image 687
JWood Avatar asked Jun 22 '26 23:06

JWood


1 Answers

Forcing android to not restart the Activity, adding android:configChanges into AndroidManifest.xml, is wrong.

Keeping this in mind:

What could be causing the stack to be empty after rotate?

Due of the Android behaviour, when you rotate the screen the MainActivity will be restarted. You can override this behaviour with the android:configChanges option of the AndroidManifest file.

android:configChanges="orientation|screenSize"
like image 110
Blackbelt Avatar answered Jun 25 '26 12:06

Blackbelt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!