Intent flag 与启动模式的对应关系
Activity有四种启动模式:
1.standard(标准) 2.singleTop 3.singleTask 4.singleInstance
标识某个Activity的启动模式,有两种方式:
1.一种是通过AndroidManifest.xml 2.一种是通过Intent的标识
通过AndroidManifest.xml来标识:
- <activity android:name=".Activity1"
- android:launchMode="standard"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
通过Intent的Flag来标识:
------------------------------------------------------------------------------------------------
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 与启动模式的对应关系的更多相关文章
- (转)Intent flag 与启动模式的对应关系
原文地址:http://www.cnblogs.com/ttylinux/p/4069513.html Activity有四种启动模式: 1.standard(标准) 2.singleTop ...
- Android中的启动模式(下)
在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的入门篇Android的启动模式(上 ...
- android启动模式2
Android中的启动模式(下) 在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的 ...
- Activity启动模式 Tasks和Back Stack
http://www.cnblogs.com/mengdd/archive/2013/06/13/3134380.html Task是用户在进行某项工作时需要与之交互的一系列activities的集合 ...
- Android的启动模式(下)
Android中的启动模式(下) 在这篇文章中,我会继续跟大家分享有关于Android中启动模式的相关知识.当然,如果对这个启动模式还不完全了解或者没有听过的话,可以先看看我之前写的有关于这个知识点的 ...
- Android进阶--Acticivity的启动模式
一.引言 我们在多次启动同一个Activity时,系统默认会重复创建多个实例,这样看上去便十分的愚蠢,所以android在设计时提供了启动模式来修改系统的默认行为.目前有四种启动模式:standard ...
- Android:图解四种启动模式 及 实际应用场景解说
在一个项目中会包括着多个Activity,系统中使用任务栈来存储创建的Activity实例,任务栈是一种“后进先出”的栈结构.举个栗子,若我们多次启动同一个Activity.系统会创建多个实例依次放入 ...
- Android之Activity系列总结(三)--Activity的四种启动模式
一.返回栈简介 任务是指在执行特定作业时与用户交互的一系列 Activity. 这些 Activity 按照各自的打开顺序排列在堆栈(即返回栈,也叫任务栈)中. 首先介绍一下任务栈: (1)程序打开时 ...
- Android_Activity的生命周期、跳转方式及参数传递、启动模式。
Activity的生命周期: onCreat ,onStart,onResume,onPause,onRestart,onStop,onDestroy Activity之间的跳转分为显式跳转和隐式跳转 ...
随机推荐
- photobooth.js jquery
<div id="example" class="photobooth" style="width:758px;height:400px&quo ...
- HTML学习体会
HTML介绍 华丽的网页界面,都是由静态网页和一些动态效果,插入的视频,和flash等等,不得不说,静态网页的制作,是学习网页的必经之路,可见静态网页在学习网页的前端是十分重要.静态网页主要是通过ht ...
- 一台机子上运行使用不同Java版本的多个tomcat
方法 在tomcat/bin/下创建setenv.sh并写入 export JAVA_HOME=/usr/share/jvm/jdk1..0_91 When you starting tomcat u ...
- JS判断是否是IE浏览器
前最短的IE判定借助于IE不支持垂直制表符的特性搞出来的. var ie = !+"\v1"; 仅仅需要7bytes!参见这篇文章,<32 bytes, ehr ... 9, ...
- Winform添加Label
Info from : http://www.csharpwin.com/csharpspace/6253r7952.shtml 本例子主要是介绍如何在 C#开发WinForm中加入一个组件,如果你想 ...
- Python之路【番外篇】回顾&类的静态字段
回顾 回顾:字符串.列表.字典的修改关于内存的情况 一.字符串 str1 = 'luotianshuai' str2 = str1 print id(str1) print id(str2) prin ...
- MYSQL调优
4核8G内存配置文件 explain SQL 查看SQL索引使用情况. my.cnf skip-external-locking skip-name-resolve back_log= key_buf ...
- 把字符转换为 HTML 实体
把字符转换为HTML实体:htmlentities() 把HTML实体转换回字符:html_entity_decode() 把预定义的字符 "<" (小于)和 "& ...
- C#指定日期为一年中的第几周
/// <summary> /// 获取指定时间在为一年中为第几周 /// </summary> /// <param name="dt">指定 ...
- 昨天的这个先补上--这个是关于 JQ 的移动 和 渐变特效的点击事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...