15

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 launch a new Activity and keep Fragment F_Left... how can I do it?

Can I retain Fragment F_Left state between activities?

Note that I want to launch a new Activity because Fragment F_Left is my app menu and Fragment F_Right changes completely the context of the user operations... and my app have many of operations, so it makes sense to have an Activity per operation.

I know its possible to retain Fragment within an Activity, but as Fragment life cycle is closely tied to the container Activity I don't know if this is possible keep Fragment state between Activities.

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
Axel M. Garcia
  • 5,138
  • 9
  • 27
  • 29
  • What is your main reason for wanting to try to reuse the fragment instead of reloading a new fragment with the new activity? Are you worried about UI state, just trying to be efficient, etc.? – Mark D May 17 '11 at 17:46
  • I'm worried about UI state. In my left fragment I have a Menu of operations. Every operation will need many steps to perform. And for every step a screen is displayed (Fragments placed at right). So I have grouped operations in Activities, so one Activity manage all the fragments of one operation plus the left fragment. When user changes of operation by clicking on the left fragment I want to launch the new Activity that will handle all the operation fragments, and keep the state of the left pane UI. – Axel M. Garcia May 18 '11 at 08:12

4 Answers4

34

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).

Community
  • 1
  • 1
quietmint
  • 13,885
  • 6
  • 48
  • 73
  • 10
    Good answer. Since `SavedState` implements `Parcelable`, you can also pass the state around via `Bundle.putParcelable()`, instead of sticking it in the `Application` instance. – TalkLittle Jul 14 '14 at 23:12
4

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.

Mark D
  • 3,317
  • 1
  • 26
  • 27
  • 1
    Yep! you are right, but I decided to use one Activity per operation to avoid having one single Activity managing hubndreds of fragments. Its a huge application :) and it helps to keep things separated by context. But anyway, if at the end left fragment becomes a headache I will move all to a single Activity. :) Thx4ur interest – Axel M. Garcia May 19 '11 at 08:02
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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

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.

PJL
  • 18,735
  • 17
  • 71
  • 68