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?

  • android - proguard error in android studio
  • Unknown animation name: decelerateInterpolator
  • Android Studio 3.0 Gradle sync failed: java.lang.AssertionError (in UnresolvedDependenciesReporter)
  • Up to parent activity - on Android
  • How to serve a file on sdcard using NanoHTTPD (inside Android)
  • adb devices return error: protocol fault (no status)
  • 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:

    Can't migrate project to Gradle in Android Studio
    Start Activity from preferences.xml and get the result in onActivityResult
    Regarding serialization, serialVersionUID, and incompatible class
    How can I use Tensorflow with react-native?
    How To Use Javascript to Detect When Phonegap-based Android App Loses Focus
    Reuse a standard android attribute on my custom view
  • Open Android App by sending a text message?
  • How to know whether user has changed the state of toggle button?
  • java.lang.RuntimeException: Unable to instantiate application : ClassNotFoundException (Only on X86 architecture device)
  • How to check whether a known uri file exists in Android storage?
  • “Real” testing of sms receiver
  • Comparing two drawables in android
  • 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.