activity的四种启动模式
2015-06-17 21:14
375 查看
当应用运行起来后就会开启一条线程,线程中会运行一个任务栈,当Activity实例创建后就会放入任务栈中。Activity
启动模式的设置在AndroidManifest.xml文件中,通过配置Activity的属性android:launchMode=""设置,例如:
<activity android:name=".MainActivity" android:launchMode="standard" />
1. Standared模式(默认)
"standard" (the default mode)Default. The system creates a new instance of the activity in the task from which it was started and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one
task can have multiple instances.
默认标准的启动模式, 每次startActivity都是创建一个新的activity的实例。适用于绝大大数情况
2. SingleTop模式
times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is
not an existing instance of the activity).
For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default
of type B, then a new instance of B is added to the stack, even if its launch mode is
Note: When a new instance of an activity is created, the user can press the
Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the
Back button to return to the state of the activity before the new intent arrived in
单一顶部,如果要开启的activity在任务栈的顶部已经存在,就不会创建新的实例,而是调用 onNewIntent() 方法。
应用场景: 浏览器书签。 避免栈顶的activity被重复的创建,解决用户体验问题。
Androidfest的配置:
<activity
android:name="edu.fjnu.taskstack.SecondActivity"
android:label="@string/_second"
android:launchMode="singleTop">
</activity>
例如:
若我有两个Activity名为B1,B2,两个Activity内容功能完全相同,都有两个按钮可以跳到B1或者B2,唯一不同的是B1为standard,B2为singleTop。
若我意图打开的顺序为B1->B2->B2,则实际打开的顺序为B1->B2(后一次意图打开B2,实际只调用了前一个的onNewIntent方法)
若我意图打开的顺序为B1->B2->B1->B2,则实际打开的顺序与意图的一致,为B1->B2->B1->B2。
3. SingleTask模式
Note: Although the activity starts in a new task, the
Back button still returns the user to the previous activity.
单一任务栈 , activity只会在任务栈里面存在一个实例。如果要激活的activity,在任务栈里面已经存在,就不会创建新activity,而是复用这个已经存在的activity,调用 onNewIntent() 方法,并且清空当前activity任务栈上面所有的activity应用场景:浏览器activity, 整个任务栈只有一个实例,节约内存和cpu的目的
注意: activity还是运行在当前应用程序的任务栈里面的。不会创建新的任务栈。
Androidfest的配置:
<activity
android:name="edu.fjnu.taskstack.SecondActivity"
android:label="@string/_second"
android:launchMode="singletask">
</activity>
例如:
若我的应用程序中有三个Activity,C1,C2,C3,三个Activity可互相启动,其中C2为singleTask模式,那么,无论我在这个程序中如何点击启动,如:C1->C2->C3->C2->C3->C1-C2,C1,C3可能存在多个实例,但是C2只会存在一个,并且这三个Activity都在同一个task里面。
但是C1->C2->C3->C2->C3->C1-C2,这样的操作过程实际应该是如下这样的,因为singleTask会把task中在其之上的其它Activity destory掉。
操作:C1->C2 C1->C2->C3 C1->C2->C3->C2 C1->C2->C3->C2->C3->C1 C1->C2->C3->C2->C3->C1-C2
实际:C1->C2 C1->C2->C3 C1->C2 C1->C2->C3->C1 C1->C2
4. SingleInstance模式
单一实例,整个手机操作系统里面只有一个实例存在。不同的应用去打开这个activity共享 公用的同一个activity。他会运行在自己单独,独立的任务栈里面,并且任务栈里面只有他一个实例存在。
Androidfest的配置:
<activity
android:name="edu.fjnu.taskstack.SecondActivity"
android:label="@string/_second"
android:launchMode="singleInstance">
</activity>
应用场景:呼叫来电界面 InCallScreen
当应用运行起来后就会开启一条线程,线程中会运行一个任务栈,当Activity实例创建后就会放入任务栈中。Activity
启动模式的设置在AndroidManifest.xml文件中,通过配置Activity的属性android:launchMode=""设置,例如:
<activity android:name=".MainActivity" android:launchMode="standard" />
1. Standared模式(默认)
"standard" (the default mode)Default. The system creates a new instance of the activity in the task from which it was started and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one
task can have multiple instances.
默认标准的启动模式, 每次startActivity都是创建一个新的activity的实例。适用于绝大大数情况
2. SingleTop模式
"singleTop"If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its
onNewIntent()method, rather than creating a new instance of the activity. The activity can be instantiated multiple
times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is
not an existing instance of the activity).
For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default
"standard"launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is
"singleTop", the existing instance of D receives the intent through
onNewIntent(), because it's at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity
of type B, then a new instance of B is added to the stack, even if its launch mode is
"singleTop".
Note: When a new instance of an activity is created, the user can press the
Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the
Back button to return to the state of the activity before the new intent arrived in
onNewIntent().
单一顶部,如果要开启的activity在任务栈的顶部已经存在,就不会创建新的实例,而是调用 onNewIntent() 方法。
应用场景: 浏览器书签。 避免栈顶的activity被重复的创建,解决用户体验问题。
Androidfest的配置:
<activity
android:name="edu.fjnu.taskstack.SecondActivity"
android:label="@string/_second"
android:launchMode="singleTop">
</activity>
例如:
若我有两个Activity名为B1,B2,两个Activity内容功能完全相同,都有两个按钮可以跳到B1或者B2,唯一不同的是B1为standard,B2为singleTop。
若我意图打开的顺序为B1->B2->B2,则实际打开的顺序为B1->B2(后一次意图打开B2,实际只调用了前一个的onNewIntent方法)
若我意图打开的顺序为B1->B2->B1->B2,则实际打开的顺序与意图的一致,为B1->B2->B1->B2。
3. SingleTask模式
"singleTask"The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its
onNewIntent()method, rather than creating a new instance. Only one instance of the activity can exist at a time.
Note: Although the activity starts in a new task, the
Back button still returns the user to the previous activity.
单一任务栈 , activity只会在任务栈里面存在一个实例。如果要激活的activity,在任务栈里面已经存在,就不会创建新activity,而是复用这个已经存在的activity,调用 onNewIntent() 方法,并且清空当前activity任务栈上面所有的activity应用场景:浏览器activity, 整个任务栈只有一个实例,节约内存和cpu的目的
注意: activity还是运行在当前应用程序的任务栈里面的。不会创建新的任务栈。
Androidfest的配置:
<activity
android:name="edu.fjnu.taskstack.SecondActivity"
android:label="@string/_second"
android:launchMode="singletask">
</activity>
例如:
若我的应用程序中有三个Activity,C1,C2,C3,三个Activity可互相启动,其中C2为singleTask模式,那么,无论我在这个程序中如何点击启动,如:C1->C2->C3->C2->C3->C1-C2,C1,C3可能存在多个实例,但是C2只会存在一个,并且这三个Activity都在同一个task里面。
但是C1->C2->C3->C2->C3->C1-C2,这样的操作过程实际应该是如下这样的,因为singleTask会把task中在其之上的其它Activity destory掉。
操作:C1->C2 C1->C2->C3 C1->C2->C3->C2 C1->C2->C3->C2->C3->C1 C1->C2->C3->C2->C3->C1-C2
实际:C1->C2 C1->C2->C3 C1->C2 C1->C2->C3->C1 C1->C2
4. SingleInstance模式
"singleInstance".Same as
"singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.
单一实例,整个手机操作系统里面只有一个实例存在。不同的应用去打开这个activity共享 公用的同一个activity。他会运行在自己单独,独立的任务栈里面,并且任务栈里面只有他一个实例存在。
Androidfest的配置:
<activity
android:name="edu.fjnu.taskstack.SecondActivity"
android:label="@string/_second"
android:launchMode="singleInstance">
</activity>
应用场景:呼叫来电界面 InCallScreen
相关文章推荐
- Android中的广播Broadcast详解
- 收集常用的.net开源项目
- 【Java基础】——集合类(下)
- Session 与 Cookie
- SocketIO --- iOS使用篇
- PHP PDO 连接数据库方法 记录下
- sql server获取当前日期
- iOS开发-71】解决方案:Attempting to badge the application icon but haven't received permiss...
- 使用 Flex 库项目---打包swc
- js跨域问题
- SSRS 实现多个文件同时上传和下载到ReportManager的功能
- ASP.NET如何定时执行任务
- mysql添加用户和入门命令
- Proxifier v3.28
- Database File Management ->> Shrink Data File
- Windows Server 2008的远程控制修改端口,谨防非法远程连接
- Android内核开发:学会分析系统的启动log
- 剑指offer 26 - 复杂链表的复制
- 二叉树的镜像
- laravel, Composer和autoloading