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

  • How to access Fragment's child views inside fragment's parent Activity?
  • Setting android:animateLayoutChanges programmatically
  • Resizing layouts programmatically (as animation)
  • AssertionFailedError in ApplicationTestCase.createApplication() in newer Android versions when using MockContext
  • Unable to create converter for my class in Android Retrofit library
  • How to manage `startActivityForResult` on Android?
  • 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.

    Related posts:

    The following classes could not be instantiated: -
    Facebook Canvas App in WebView not working on Android (in native facebook app)
    Android gradle build: running assembleDebug makes release tasks of project dependencies being called
    Webview load html from assets directory
    Send request over Mobile data when WIFI is ON.(Android L)
    DialogFragment not showing after adding Alert Window
  • How to display some value on seekbar as default?
  • Duplicate ID, tag null, or parent id with another fragment for
  • Different regular expression result in Java SE and Android platform
  • How does Xamarin iOS and Android work?
  • Rotate View Hierarchy 90 degrees
  • Help passing an ArrayList of Objects to a new Activity
  • 4 Solutions collect form web for “Retain Fragment state between Activities”

    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.

    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();
      fragmentManager.beginTransaction().add(, 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).

    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.

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.