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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/android/315651.html

猜你在找的Android相关文章