在Android中修改后栈

前端之家收集整理的这篇文章主要介绍了在Android中修改后栈前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的 Android应用程序中修改后台堆栈:

现在,这是流程:

A – > B – > C – > D – > E – > F

我希望能够修改后台堆栈,这样当用户进入活动F时,D和E将从堆栈中删除.所以流量是F – > C如果用户击中后面.

此外,从F,用户能够进入活动B,这也应该擦除C,D,E和F.

我已经看到了一些关于能够清除堆栈或删除顶部项目的信息,但是我想在触发活动时从堆栈中删除一些项目.

感谢任何帮助,非常感谢.

解决方法

您可以使用从F到C的标志intent.FLAG_ACTIVITY_CLEAR_TOP构建一个intent.然后您必须使用intent调用startActivity()并触发它以发生onBackPressed或类似的东西.
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);
}

代码示例源:https://stackoverflow.com/a/9398171/1003511

原文链接:https://www.f2er.com/android/308875.html

猜你在找的Android相关文章