1,Android中什么是Task和Back Stack html
Task能够理解为一个进程,启动一个应用就回启动一个进程,系统会为该应用分配16MB的空间来共该应用使用。Back Stack是以堆栈的形式管理一个应用启动的全部的Activity,每启动一个Activity都会把新的Activity放到栈顶,若栈顶Activity销毁(经过onBackPressed或者finish)以后,栈顶Activity下方的Activity会从新显示,栈的机制就是后进先出(last in, fisrt out)。 android
当一个Task中全部Activity都销毁时,这个Task则销毁,这个应用则关闭(除非还有一个Task启动了此应用)。 app
对于Android系统来讲,后台能够运行多个Task任务,多个Task也是以堆栈行的进行管理。 spa
先列出以下疑问,后续会逐一解答: htm
<1>如何使一个应用的全部Activity只在一个Task中? 进程
<2>当应用A调用应用B的时候,应用B会在应用A的Task中仍是会在新建的一个Task中? ip
<3>Android系统会在Activity被stopped以后保存这个Activity的状态(如输入框中内容,勾选状态等),当这个Activity被resume的时候,会显示被stopped以前的数据,可是有一种特殊状况:Android系统内存不足时,会将这个Activity被destory,这个Activity只能被create,以前的数据会出现丢失,若是想避免这种现象,能够在onSaveInstanceState()方法保存数据,在Activity被create或resume的时候将历史数据从新展现出来。 内存
2,Activity的运行模式(launch mode)/启动方式: ci
<1>manifest配置式launchMode。 element
standard(默认值)--永久建立
singTop--只要栈顶不是这个Activity的实例就永久建立
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().
singTask--若是在一个Task中已经建立了这个Activity时,系统将不会再在这Task中建立这个Activity,而是经过onNewIntent()方法从新调用这个Activity。
Note: Although the activity starts in a new task, theBack button still returns the user to the previous activity.
singInstance--若是这个Activity已经存在,系统中全部的Task将公用这个Activity。
<2>Intent.FLAG方式,会覆盖配置式,优先级高于配置式。
FLAG_ACTIVITY_NEW_TASK:若是Activity将启动的Activity已经存在于Task中,则经过onNewIntent()直接将这个Activity移至栈顶,不然就会在当前Task建立这个Activity。等价于singTask
FLAG_ACTIVITY_SING_TOP:等价于singTop.
FLAG_ACTIVITActivityY_CLEAR_TOP:若是Task中这个Activity已经存在且不位于栈顶,则将栈中 位于这个Activity上方的全部Activity销毁,经过onNewIntent()将这个Activity移至栈顶。
FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.
Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard".
3,Mainfest中各个标签
Tip: If an .apk file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity attribute to assign different affinities to the activities associated with each "application".
Android系统一个特殊行为:当一个应用启动以后,长时间不操做,系统则会销毁这个应用所在的Task中的全部Activity除了root activity(这个activity是栈底activity仍是启动activity有待确认),当返回这个应用时,则显示这个root activity。
设置root activity的属性alwaysRetainTaskState为true,上述特殊行为将不会执行,即保留这个应用所在的Task中的全部Activity。
设置root activity的属性clearTaskOnLaunch为true,不管离开应用多长时间,都销毁这个应用所在的Task中的全部Activity,进入应用初始状态,换句话说,clearTaskOnLaunch与alwaysRetainTaskState是相对的。
设置root activity的属性finishOnTaskLaunch为true,finishOnTaskLaunch与clearTaskOnLaunch优势类似,finishOnTaskLaunch应用于一个activity,而clearTaskOnLaunch应用于一个Task。
For those cases where you don't want the user to be able to return to an activity, set the <activity> element's finishOnTaskLaunch to "true" .
上述内容仅为理论,有待实践。