2

I have an Activity that handles many Fragments, and, for backstack management, I have a custom stack, where I manage show / hide for Fragments. My code and navigation work perfectly.

Right now, I am implementing the application theme change by a Button in the Configuration Fragment. For this I am using the method Activity.Recreate (); for the change of the theme and, the data of the Configuration Fragment is retained with the same data and the theme of the application changes perfectly, but the BackStack of Fragments disappears, reason why, when pressing the back button, it leaves the application, instead of sending me back to the Fragment or Previous Activity, from where I accessed the Configuration Fragment.

What is the best way to maintain the backstack of my Activity? This is possible?

Important: only when Activity.Recreate(); is called, because if the Activity is destroyed by any other way, I do not want the BackStack back, I want my Activity clean.

Additional:

  • The orientation setting of my application is in portrait mode.
  • The launchMode of my Activity is singleTask, it must be so for the type of application I am doing.
Andrespengineer
  • 264
  • 2
  • 17
  • Usually, when you change the theme of an application in native android or the language it is always suggested that you go back to the homepage of your application, By that I mean to start the application over, this is the only correct way of doing it, if you use recreate on the other hand it will destroy your current activity instance hence clear all the fragment backstack, you might say you will save the fragment backstack somewhere and use the bundle instances to recreate them as they were before (I wouldn't ever recommend this because it is prone to human error and wont have the desired op) – FreakyAli Apr 04 '19 at 06:31

1 Answers1

1

From onCreate documentation and this answer.

Add the following logic to your code:

public void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState == null) { 
        // savedInstanceState will be null only when creating the activity for the first time
        backstack = new BackStack(); //init your backstack
    } else {
      // there is a chance that your backstack will be already exists at this point
      // if not:
      // retrieve the backstack with savedInstanceState.getSerializable("stack")
    }
}

And just clear the stack when changing theme, before calling recreate()

// changing theme detected
bacstack.clear();
backstack = null;
recreate();

To save the stack between destruction (onDestroy) and recreation (onCreate) of your activity, Use this method:

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    if (backstack != null) // the check isn't necessary, you can just put a null in the bundle
        outState.putSerializable("stack", backstack);
}

the official guide for saving UI state

The onSaveInstanceState method helps your activity to Survive Configuration change and System-initiated process death. link

Community
  • 1
  • 1
ronginat
  • 1,910
  • 1
  • 12
  • 23