Restore back in Android

I just implemented a way to change the theme in my application. Clicking the button sets the value to SharedPreferences, then the activity is recreated, and the theme changes due to the flag.

The problem is how to handle backstack. A simple recreation of activity is not very good, because pressing the hardware return button will resume the previous operation (with the old theme installed), whereas if the user did the following:

Start in action A> B> C> Press the theme change button> Press the back button

Then I want them to be returned to Activity B with the new theme correctly applied . The best solution I have found so far is to recreate the backstack based on the hierarchy of objects as follows:

Intent intent = new Intent(); intent.setClass(activity, activity.getClass()); TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity); stackBuilder.addNextIntentWithParentStack(intent); stackBuilder.startActivities(new Bundle()); 

However, the Activity hierarchy in our application is not defined (that is, C may have several parents), so the above code may lead to the user returning to an unexpected action after clicking the "Back" button.

Is there a clean way to rebuild the back-stack based on the actual back-stack and not the Activity hierarchy?

+6
source share

All Articles