现在,这是流程:
A – > B – > C – > D – > E – > F
我希望能够修改后台堆栈,这样当用户进入活动F时,D和E将从堆栈中删除.所以流量是F – > C如果用户击中后面.
此外,从F,用户能够进入活动B,这也应该擦除C,D,E和F.
我已经看到了一些关于能够清除堆栈或删除顶部项目的信息,但是我想在触发活动时从堆栈中删除一些项目.
感谢任何帮助,非常感谢.
解决方法
Intent i = new Intent(this,C.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i)
请参阅此答案,该答案还涉及确保在导航回来时不会重新启动C:https://stackoverflow.com/a/11347608/1003511
FLAG_ACTIVITY_CLEAR_TOP将执行的操作是返回堆栈上最新的活动C实例,然后清除其上方的所有内容.但是,这可能会导致重新创建活动.如果要确保它与活动的实例相同,请同时使用FLAG_ACTIVITY_SINGLE_TOP.从文档:
The currently running instance of activity B in the above example will
either receive the new intent you are starting here in its
onNewIntent() method,or be itself finished and restarted with the new
intent. If it has declared its launch mode to be “multiple” (the
default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same
intent,then it will be finished and re-created; for all other launch
modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be
delivered to the current instance’s onNewIntent().
编辑:这是一个类似于你想要做的代码示例:
@Override public boolean onKeyDown(int keyCode,KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Intent a = new Intent(this,C.class); a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(a); return true; } return super.onKeyDown(keyCode,event); }