微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 根据指南处理“向上”导航的正确方法

我完全赞同下面的导航

想象一下,Book详细信息是在BookDetailActivity的不同实例中创建的.

在book2详细信息中按下之前的堆栈是:

> BookDetailActivity(第2册 – 你在这里)
> BookDetailActivity(第1册)
> AllBooksActivity

如果我按照guidelines我将使用:

Intent parentActivityIntent = new Intent(this,AllBooksActivity.class);
        parentActivityIntent.addFlags(
                Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(parentActivityIntent);
        finish();

但是这个代码的大问题是BookDetailActivity(第1册)还活着!

在“向上”之后按下后退按钮将带来第1册的细节.

如何杀死原始AllBooksActivity和我按下的活动之间的所有BookDetailActivity?

解决方法

related guidelines article注意到以下内容

Implementation Note: As a best practice,when implementing either Home
or Up,make sure to clear the back stack of any descendent screens.
For Home,the only remaining screen on the back stack should be the
home screen. For Up navigation,the current screen should be removed
from the back stack,unless Back navigates across screen hierarchies.
You can use the FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK
intent flags together to achieve this.

由于你正在这样做,BookDetailActivity1应该在FLAG_ACTIVITY_CLEAR_TOP之前关闭.如果它可以存活并且按下Back后显示的唯一方法是它是否在AllBooksActivity之前启动.

至于不需要FLAG_ACTIVITY_NEW_TASK(由android开发人员的答案建议):

When using this flag,if a task is already running for the activity
you are Now starting,then a new activity will not be started;
instead,the current task will simply be brought to the front of the
screen with the state it was last in.

…所以如果您的活动存在,它将无法启动新任务.

原文地址:https://www.jb51.cc/android/315651.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐