Clear backstack in Android pre Honeycomb?

2019-05-26 07:00发布

问题:

Is there an easy way or another workaround to delete the backstack in Android pre Honeycomb(before API level 11)? People suggest using the FLAG_ACTIVITY_CLEAR_TOP in conjunction with FLAG_ACTIVITY_NEW_TASK when starting a new activity, but this does only delete the stack on top of my current position, not the stack under my position. It should not be that hard to start from a fresh task. Some ideas around this? I can not use FLAG_ACTIVITY_CLEAR_TASK because I need to support those versions beneath api level 11. Is there an equivalent to FLAG_ACTIVITY_CLEAR_TASK that clears the whole navigation backstack. Or something similiar to FLAG_ACTIVITY_REORDER_TO_FRONT that reorders to back, and then I can clear everything on top of it. All suggestions are highly appreciated:) Thanks!

回答1:

You can also use a broadcast listener.

Just make a broadcast listener in all the activities with a "STRING" to recognise.

Whenever you want to delete all the activities, fire the intent.

Those activities that have registered for the above listener (to be done by depending on which activites you wanna finish), will intercept that and will get finish.

This is gonna work in any release of the Android.



回答2:

Why don't you create a static ArrayList of all the previous activities and clear the ones you don't want whenever you want using the activity.destroy()



回答3:

you are looking for this:

Intent intent = new Intent(activity, activityClass);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
activity.startActivity(mainIntent);

use android compatibillity lib from google - found in the sdk.

depending on your API version it might also be:

Intent mainIntent = IntentCompat.makeMainActivity(cn);