Skip to content Skip to sidebar Skip to footer

Retain Fragment State Between Activities

Its possible to retain a Fragment between Activities? Lets say I have Activity A with Fragment F_Left placed at the left and Fragment F_Right placed at the right. If I want to laun

Solution 1:

Since API Level 13 (HONEYCOMB_MR2, June 2011), you can save and restore the state of a fragment across activities.

  • To save the state, use FragmentManager.saveFragmentInstanceState(), providing a reference to the Fragment whose state you wish to save. The Fragment must be attached at the time you attempt to save its state.

  • To restore the state, use Fragment.setInitialSavedState() with the return value when you instenciate the same Fragment.

    myFragment = new MyFragment();
    myFragment.setInitialSavedState(appState.getMyFragmentState());
    fragmentManager.beginTransaction().add(R.id.container, myFragment).commit();
    

You can persist the SavedState object across activities as you would any other object; one way is to subclass Application as shown above (appState is the instance of our subclass).


Solution 2:

Based on your response to my comment, I have a slightly different answer. It may not end up being the best answer in your specific situation, I'll let you decide that. :)

Right now you are bundling your fragments in activities because that is what made sense to you, but really, you can probably treat the entire process as one activity and use fragment transactions to hide & show (or create and destroy) fragments as needed.

Since you won't be creating and destroying activities, your menu fragment on the left will be left untouched, and you won't have any problems with its UI state. The set of operations you want to run (which no doubt includes all sorts of different fragments on the right) does not need to be launched in a new activity - but you will have to find a way to manage the logic you need for the fragment transactions (either in your one über-activity or in some kind of OperationsManager class).

I think this will end up being a lot smoother for the users of your application since the single activity just remains running - and you are only changing the parts that actually need to change.


Solution 3:

If I want to launch a new Activity and keep Fragment F_Left... how can I do it?

Don't launch a new activity.

Can I retain Fragment F_Left state between activities?

Not automatically. It is not the same fragment. You would pass data between the activities for use by the fragment no differently than you would without any fragments at all.


Solution 4:

To potentially answer your original question, if you fire off another activity then I believe that you can save your fragment from your first activity by calling FragmentManager::putFragment(...) when onSaveInstanceState(...) is called and then getting it back later, e.g. in onCreate(...).

However, I have to agree with Mark D's response.

Incidentally I'm doing something similar in that I have a dual pane setup whereby the left pane if fixed with a number of options with each option invoking a different fragment in the right pane. Furthermore selecting an entry in the right pane can result in the right fragment being replaced by another one.

However, I have taken the approach whereby by left fragment is only responsible for displaying and handling responses from the immediate fragment which appears in the right hand pane. Furthermore each right-hand fragment is then responsible for 'replacing' itself with a new fragment and handling results sent back to it. I'm using setTargetFragment, getTargetFragment, and calling onto the target fragment's onActivityResult method to pass results back.

For me the approach I've taken is no different from when my app runs on a phone with a single pane whereby the initial option's activity only knows about the activies it fires off and subsequently these new ones fire off further activies which they know about.

It should be mentioned that my activity in my dual pane app doesn't really do much apart from loading the left pane fragment and I can't quite see the need for a single activity to ever have to manage hundreds of fragments.


Post a Comment for "Retain Fragment State Between Activities"