前言
在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作。在Android中 Activity的启动模式决定了Activity的启动运行方式。
Android总Activity的启动模式分为四种:
- standard
- singleTop
- singleTask
- singleInstance
Activity启动模式设置:
启动模式介绍
Activity的四种启动模式
1.standard:每次激活Activity时都会创建Activity,并放入任务栈中。
2.singleTop:如果在任务的栈顶正好存在该Activity的实例, 就重用该实例,否者就会创建新的实例并放入栈顶(即使栈中已经存在该Activity实例,只要不在栈顶,都会创建实例)。
3.singleTask:如果在栈中已经有该Activity的实例,就重用该实例(会调用实例的onNewIntent())。重用时,会让该实例回到栈顶,因此在它上面的实例将会被移除栈。如果栈中不存在该实例,将会创建新的实例放入栈中。
4.singleInstance:在一个新栈中创建该Activity实例,并让多个应用共享该栈中的该Activity实例。一旦该模式的Activity的实例存在于某个栈中,任何应用再激活该Activity时都会重用该栈中的实例,其效果相当于多个应用程序共享一个应用,不管谁激活该Activity都会进入同一个应用中。
其中standard是系统默认的启动模式。
任务栈
每个应用都有一个任务栈,是用来存放Activity的,功能类似于函数调用的栈,先后顺序代表了Activity的出现顺序;比如Activity1–>Activity2–>Activity3,则任务栈为:
启动模式图解
(1)standard:每次激活Activity时(startActivity),都创建Activity实例,并放入任务栈;
(2)singleTop:如果某个Activity自己激活自己,即任务栈栈顶就是该Activity,则不需要创建,其余情况都要创建Activity实例;
(3)singleTask:如果要激活的那个Activity在任务栈中存在该实例,则不需要创建,只需要把此Activity放入栈顶,并把该Activity以上的Activity实例都pop;
(4)singleInstance:如果应用1的任务栈中创建了MainActivity实例,如果应用2也要激活MainActivity,则不需要创建,两应用共享该Activity实例;
SingTask的应用:
可以用来退出整个应用。将主Activity设为SingTask模式,然后在要退出的Activity中转到主Activity,然后重写主Activity的onNewIntent函数,并在函数中加上一句finish。
附:退出单个Activity方法:
- 1.调用finish
- 2.杀死该进程:killprocess(Process.mId)
- 3.终止正在运行的虚拟机:system.exit()
各种启动模式详解
下面通过代码解释各种模式的运行机制
1.standard
|
|
初始化界面如下:
当点击按钮时,会创建新的Activity,通过TextView@后16进制数的显示即可看出,点击两次分别界面如下:
此时,我们分析栈内部的运行机制:(依次从栈顶向上)
因此,这种Standard模式是每次都会创建新的Activity对象,当点击返回按钮时,他会将栈顶(当前Activity)消灭,然后跳到下一 层,例如如果现在Activity是44ed8c50,那么当我们点击返回时Activity会变为44f28a48,不过此时在这个Activity中 再次点击按钮创建对象时,它会另外创建新的Activity对象,这种模式可能大多数情况下不是我们需要的,因为对系统性能的消耗过大。
下面我们介绍两种能使用当前栈中Activity的启动模式:
2. singleTop
从上面的解释中即可知道,在每次使用新的Activity时会自动检测栈顶的当前Activity是否是需要引用的Activity,如果是则直接引用此Activity,而不会创建新的Activity。
我们在刚才的界面中加入一个”启动singletop模式”按钮,当点击时出现我们创建的singletop中,在Activity singletop中有一个按钮,启动singletop模式,表示启动当前Activity,由于我们在清单文件中配置Activity的启动模式为 singleTop,因此此时不会再创建而是利用当前栈顶的singleTop Activity:
界面初始化:
点击”启动singleTop模式”按钮:
我们分析它的运行机制,可知,当程序运行到此时,栈中的数据形式为:
当我们在上面界面中点击”启动singleTop模式”按钮时,由于此Activity设置的启动模式为singleTop,因此它首先会检测当 前栈顶是否为我们要请求的Activity对象,经验证成立,因此它不会创建新的Activity,而是引用当前栈顶的Activity。
虽然它不会创建新的Activity对象,不过它每次回调用onNewIntent()方法:
我们为此方法编写代码输出当前日期,则在每次点击上面按钮时会输出当前日期。
3.singleTask
此启动模式和singleTop在名字上即可看出区别,即singleTop每次只检测当前栈顶的Activity是否是我们需要请求创建的,而 singleTask则会检测栈中全部的Activity对象,从上向下,如果检测到是我们所请求的则会消灭此Activity对象上面的对象,直接把检测到的我们需要的Activity置为栈顶。
我们创建一个SingleTaskActivity,此界面中包含一个启动MainActivity和启动SingleTaskActivity 按钮。
初始化:
点击”启动singleTask模式”按钮:
在此界面中点击第二个按钮”启动singleTask模式”按钮,根据定义会检测当前栈中是否有此Activity对象,因此显示的还是当前的 Activity,不会重新创建;
再点击”启动Standard模式”按钮,由于MainActivity的启动模式为standard,所以在此会重新创建一个 MainActivity对象:
此时栈中数据格式为:
当在上面界面中点击”启动singleTask模式”按钮时,由于检测到当期栈中第二个为我们要创建的Activity,会将最上面的 MainActivity消灭,然后将SingleTaskActivity设置为栈顶:
4.SingleInstance
此启动模式和我们使用的浏览器工作原理类似,我们都知道在多个程序中访问浏览器时,如果当前浏览器没有打开,则打开浏览器,否则会在当前打开的浏 览器中访问。此模式会节省大量的系统资源,因为他能保证要请求的Activity对象在当前的栈中只存在一个。
退出整个应用
通过singleTop的原理,退出整个应用。具体实现是添加退出类:
Android的窗口类提供了历史栈,我们可以通过stack的原理实现,我们在D窗口打开A窗口时在Intent中直接加入标志Intent.FLAG_ACTIVITY_CLEAR_TOP,再次开启A时将会清除该进程空间的所有Activity。 在D中使用下面的代码:
关掉自己 在A中加入代码:
|
|
A的Manifest.xml配置成android:launchMode=”singleTop”
原理总结:
一般A是程序的入口点,从D起一个A的activity,加入标识Intent.FLAG_ACTIVITY_CLEAR_TOP这个过程中会把栈中B,C,都清理掉。因为A是android:launchMode=”singleTop” 不会调用oncreate(),而是响应onNewIntent()这时候判断Intent.FLAG_ACTIVITY_CLEAR_TOP,然后把A finish()掉。 栈中A,B,C,D全部被清理。所以整个程序退出了。
Developers官网解释
android:launchMode
An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAGACTIVITY* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are:
- “standard”
- “singleTop”
- “singleTask”
- “singleInstance”
The default mode is “standard”.
As shown in the table below, the modes fall into two main groups, with “standard” and “singleTop” activities on one side, and “singleTask” and “singleInstance” activities on the other. An activity with the “standard” or “singleTop” launch mode can be instantiated multiple times. The instances can belong to any task and can be located anywhere in the activity stack. Typically, they’re launched into the task that called startActivity() (unless the Intent object contains a FLAG_ACTIVITY_NEW_TASK instruction, in which case a different task is chosen — see the taskAffinity attribute).
In contrast, “singleTask” and “singleInstance” activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.
The “standard” and “singleTop” modes differ from each other in just one respect: Every time there’s a new intent for a “standard” activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a “singleTop” activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created. In other circumstances — for example, if an existing instance of the “singleTop” activity is in the target task, but not at the top of the stack, or if it’s at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.
Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity’s launch mode. If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. The navigation intent is received by the parent activity’s onNewIntent() method. If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.
The “singleTask” and “singleInstance” modes also differ from each other in only one respect: A “singleTask” activity allows other activities to be part of its task. It’s always at the root of its task, but other activities (necessarily “standard” and “singleTop” activities) can be launched into that task. A “singleInstance” activity, on the other hand, permits no other activities to be part of its task. It’s the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.
Use Cases | Launch Mode | Multiple Instances? | Comments |
---|---|---|---|
Normal launches for most activities | “standard” | Yes | Default. The system always creates a new instance of the activity in the target task and routes the intent to it. |
“singleTop” | Conditionally | If an instance of the activity already exists at the top of the target 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. | |
Specialized launches(not recommended for general use) | “singleTask” | No | The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one. |
“singleInstance” | No | 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. |
As shown in the table above, standard is the default mode and is appropriate for most types of activities. SingleTop is also a common and useful launch mode for many types of activities. The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.
Regardless of the launch mode that you choose, make sure to test the usability of the activity during launch and when navigating back to it from other activities and tasks using the Back button.
For more information on launch modes and their interaction with Intent flags, see the Tasks and Back Stack document.
上面即为Android中的四种启动模式以及应用场景,我们在开发Android项目时会经常使用到,巧妙设置Activity的启动模式会节省系统开销和程序运行效率。