Activity有四种启动模式:

1.standard(标准)    2.singleTop    3.singleTask  4.singleInstance

标识某个Activity的启动模式,有两种方式:

1.一种是通过AndroidManifest.xml    2.一种是通过Intent的标识

通过AndroidManifest.xml来标识:

  1. <activity android:name=".Activity1"
  2. android:launchMode="standard"
  3. android:label="@string/app_name">
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. </activity>

通过Intent的Flag来标识:

FLAG_ACTIVITY_NEW_TASK

FLAG_ACTIVITY_SINGLE_TOP

FLAG_ACTIVITY_CLEAR_TOP

------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_NEW_TASK---没有对应的启动模式,它的部分特征与singleTask类似。

public static final int FLAG_ACTIVITY_NEW_TASK

Added in API level 1

If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order. See Tasks and Back Stack for more information about tasks.

This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.

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. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

This flag can not be used when the caller is requesting a result from the activity being launched.

Constant Value: 268435456 (0x10000000)

使用这个特征的Intent,系统会为被启动的Activity创建一个新的task,然后被启动的Activity存放在回退栈的底部。

实验:

1.在同一个应用中:

在启动Acitivyt时,设定FLAG_ACTIVITY_NEW_TASK标识

Intent newTask = new Intent(MainActivity.this ,com.intentflag.activity.ActivityNewTaskFlag.class);

newTask.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity( newTask);

效果如图:

由上两个图,可以发现,它们的taskId都是一样的,这说明,使用FLAG_ACTIVITY_NEW_TASK标识时,

在同一个应用中,并没有上述描述的效果,会启动一个新的TASK来存放被启动的Activity实例。

从times值得变化来看,如果当前task中,有被启动Activity的实例,那么,即使使用了FLAG_ACTIVITY_NEW_TASK标识,系统依然会创建一个新的实例。

2.在两个不同的应用中:

1).在使用FLAG_ACTIVITY_NEW_TASK标识的情况下

这是被启动Activity所在的应用:

这是启动者所在的应用:

成功启动后:

然后,再按下返回键:

可以知道:1.被启动的Activity是与启动者处于不同的task中的。2.系统会创建一个新的被启动Activity实例。

3.新的Activity是处在一个新的task的顶部,如果该task已经存在的话,则该Activity实例,位于栈顶。

2).在不使用FLAG_ACTIVITY_NEW_TASK的情况下:

从结果可以知道,如果不使用FLAG_ACTIVITY_NEW_TASK,那么,被启动的Activity将会与启动者使用同一个task,是新创建一个Activity,然后,将该Activity实例存放到与启动者使用的task中。

总结,FLAG_ACTIVITY_NEW_TASK,适合,启动哪些与被启动者不是同一个应用的Activity。一个应用,用一个task来组织它的所有Activity。

--------------------------------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_SINGLE_TOP

public static final int FLAG_ACTIVITY_SINGLE_TOP

Added in API level 1

If set, the activity will not be launched if it is already running at the top of the history stack.

Constant Value: 536870912 (0x20000000)

使用这个标识去

例子,在两个应用之间来使用:

被启动Activity,在另外一个应用中是处于栈顶的:

然后,在另外一个应用中,使用FLAG_ACTIVITY_SINGLE_TOP标识来启动:

然后,启动之后的结果是:

经过实验发现,并没有上述宣称的效果。

可以知道,是创建了一个新的Cactivity。

---------------------------------------------------------------------------------------------------------------------------------

FLAG_ACTIVITY_CLEAR_TOP

public static final int FLAG_ACTIVITY_CLEAR_TOP

Added in API level 1

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

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().

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

See Tasks and Back Stack for more information about tasks.

Constant Value: 67108864 (0x04000000)

例子:

在默认情况下,只使用了FLAG_ACTIVITY_CLEAR_TOP标识

根据文档的描述,A->B->C->D,要启动B这个Activity,然后,使用的标识是FLAG_ACTIVITY_CLEAR_TOP,

然后,回退栈中的其它Activity会被清除掉,并且,B也会被销毁掉,然后重新创建。

最后的结果是:A-->B(新创建的)

实验:

然后,按下返回键,结果是返回到A,这说明,B,C,D都被清除掉了,栈顶是新的B。

这个效果,与文档所说明的效果是相同的。

Intent flag 与启动模式的对应关系的更多相关文章

  1. (转)Intent flag 与启动模式的对应关系

    原文地址:http://www.cnblogs.com/ttylinux/p/4069513.html Activity有四种启动模式: 1.standard(标准)    2.singleTop   ...

  2. Android中的启动模式(下)

    在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的入门篇Android的启动模式(上 ...

  3. android启动模式2

    Android中的启动模式(下) 在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的 ...

  4. Activity启动模式 Tasks和Back Stack

    http://www.cnblogs.com/mengdd/archive/2013/06/13/3134380.html Task是用户在进行某项工作时需要与之交互的一系列activities的集合 ...

  5. Android的启动模式(下)

    Android中的启动模式(下) 在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的 ...

  6. Android进阶--Acticivity的启动模式

    一.引言 我们在多次启动同一个Activity时,系统默认会重复创建多个实例,这样看上去便十分的愚蠢,所以android在设计时提供了启动模式来修改系统的默认行为.目前有四种启动模式:standard ...

  7. Android:图解四种启动模式 及 实际应用场景解说

    在一个项目中会包括着多个Activity,系统中使用任务栈来存储创建的Activity实例,任务栈是一种“后进先出”的栈结构.举个栗子,若我们多次启动同一个Activity.系统会创建多个实例依次放入 ...

  8. Android之Activity系列总结(三)--Activity的四种启动模式

    一.返回栈简介 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即返回栈,也叫任务栈)中. 首先介绍一下任务栈: (1)程序打开时 ...

  9. Android_Activity的生命周期、跳转方式及参数传递、启动模式。

    Activity的生命周期: onCreat ,onStart,onResume,onPause,onRestart,onStop,onDestroy Activity之间的跳转分为显式跳转和隐式跳转 ...

随机推荐

  1. 日志分析 第四章 安装filebeat

    在进行前面准备之后可以开始安装了,我们的安装顺序是filebeat--->logstash--->elasticsearch filebeat安装很简单,先下载filebeat,这里我们使 ...

  2. datagrid---写后台数据交互

    1.action的写法: 开头写包,此外,我们还有一个和action并列的package-info.java的文件,该文件是包的信息,media为我的文件夹里面放三个文件夹(action,bpo,ma ...

  3. 最好的vim教程莫过于vimtutor

    最好的vim教程莫过于vimtutor 直接运行vimtutor即可

  4. POI导出Excel的空值问题

    情景: 页面上的表格导出Excel,表格没数据导出的空格也要参与运算,结果在Excel上出错了 看图, 行-A.A12是没有数据的,后台代码cell.setCellValue("" ...

  5. HTML学习笔记——CSS初涉

    1>嵌入式css写法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  6. 关于敏捷开发方法(Agile Software Development)的阅读笔记

    对“敏捷开发”(Agile Software Development)这个词,我是在这学期邹欣老师<现代程序设计>课上第一次听到的,刚听到时并不知道其具体指什么,只是从字面上直觉其意思应该 ...

  7. Autofac.Integration.Mvc.Owin分析

    using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Secur ...

  8. sql语句积累

    有一个需求表(demand),每一记录就是一条需求:另外有一个报价表(quotation),每一条记录是对需求记录的报价详情. 需求表: 报价表: 我现在想得到每条需求的信息以及有多少人报价了,我们可 ...

  9. jQuery版本升级踩坑大全

    背景 -------------------------------------------------------------------------------- jQuery想必各个web工程师 ...

  10. ASP.NET MVC使用Bootstrap系列(4)——使用JavaScript插件

    阅读目录 序言 Data属性 VS 编程API 下拉菜单(dropdown.js) 模态框(modal.js) 标签页(tab.js) 工具提示(tooltip.js) 弹出框(popover.js) ...