Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retaining Fragment backstack on orientation change

CONSTRUCTION

I've got Activity that holds a "path" made of Fragments. The User goes from Fragment1 to Fragment2 and then to Fragment3 filling up the required informations. Nothing fancy here just plain backStack and .replace made on Fragments.

QUESTION

How should I retain that information when orientation changes to get my backStack back the way it was before orientation change?

IDEAS

The only idea I have is to insert into saveInstanceState the last visible Fragment and recreate things from there but it feels really hacky and I think in the long run it's going to make some major problems.

/////////////UPDATE//////////////

It looks like nowadays Android is capable of doing it on its own as long as you are following the guidelines provided by ARTICLE

like image 970
JakubW Avatar asked Feb 16 '16 08:02

JakubW


People also ask

How to retain data on orientation change?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

How to handle changes in screen orientation?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What happens when screen orientation changes in Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.


1 Answers

The backstack itself is saved and loaded automatically with the activity. Here's an article how Android does save and load activity and fragment states. What you should care of is implementing the same logic of saving and restoring states for each fragment.

like image 123
Daniel Zolnai Avatar answered Oct 16 '22 16:10

Daniel Zolnai