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.

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...