Clear backstack in Android pre Honeycomb?

こ雲淡風輕ζ 提交于 2019-12-22 18:42:55

问题


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);


来源:https://stackoverflow.com/questions/8678149/clear-backstack-in-android-pre-honeycomb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!