Application Fundamentals--Clearing the stack(堆栈清理)
Clearing the stack--堆栈清理
If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, it's as the user left it, except that only the initial activity is present. The idea is that, after a time, users will likely have abandoned what they were doing before and are returning to the task to begin something new.
翻译:如果用户长时间不操作界面,系统将会清除当前task堆栈中除了根Activity以外的所有实例,如果这时候用户回来,显示的界面是根Activity的界面。
That's the default. There are some activity attributes that can be used to control this behavior and modify it:
翻译:这是这种场景下系统清理当前task堆栈的默认策略,不过我们还是可以通过设定Activity(尤其是根Activity)的以下属性值来改变系统默认的堆栈清理策略:(事实上还有另外一种途径:通过设定intent的flag属性值来修改系统默认执行的堆栈清理策略,后面我们将会讲到。)
The alwaysRetainTaskState--总是保留当前task状态 attribute
If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period.
翻译:如果一个task的根Activity的这个属性被设定为 "true", 系统默认的task堆栈清理规则将失效,即使用户长时间离开操作界面也不会发生清理当前task堆栈中的Activity实例的系统操作。
The clearTaskOnLaunch---在发射台上清除task堆栈 attribute
the stack is cleared down to the root activity whenever the user leaves the task and returns to it. In other words, it's the polar opposite of alwaysRetainTaskState. The user always returns to the task in its initial state, even after a momentary absence.
翻译:如果一个task的根Activity的这个属性被设定为 "true",只要用户离开哪怕是短暂离开操作界面,系统将清除堆栈清除根activity以外所有实例,回到task的初始状态。这个属性的作用和alwaysRetainTaskState完全相反。
The finishOnTaskLaunch attribute
This attribute is like clearTaskOnLaunch, but it operates on a single activity, not an entire task. And it can cause any activity to go away, including the root activity. When it's set to "true", the activity remains part of the task only for the current session. If the user leaves and then returns to the task, it no longer is present.
翻译:这个属性的作用与clearTaskOnLaunch有点相似,也会导致当前task堆栈发生清理动作, 但是这个属性只会清除堆栈中的一个Activity,不会清除整个task中的Activity实例。如果某个Activity的这个属性被设定为"true", 这个Activity被请求的时候,对应的实例依然是被压入当前task堆栈响应用户,但是,如果用户离开当前task,重新再回到这个task时,这个 Activity将从这个task堆栈中清除,task堆栈中其他Activity依然保留,即使这个Activity是根Activity也将被清除。我们可以这样理解:一个task和用户交互的过程中,如果启动了一个clearTaskOnLaunch属性值是"true"的Activity实例,该实例进人当前task堆栈,如果之后用户离开该task(该task退到后台),下次用户再次启动该task的时候,该task堆栈中这个Activity实例不再存在。
There's another way to force activities to be removed from the stack. If an Intent object includes the FLAG_ACTIVITY_CLEAR_TOP flag, and the target task already has an instance of the type of activity that should handle the intent in its stack, all activities above that instance are cleared away so that it stands at the top of the stack and can respond to the intent. If the launch mode of the designated activity is "standard", it too will be removed from the stack, and a new instance will be launched to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard".
翻译:还有另外一种强制清除task堆栈中部分实例的途径:可以在intent对象中设定flag属性=FLAG_ACTIVITY_CLEAR_TOP, 如果被请求的Activity实例恰好在当前task堆栈已经存在,那么Android系统将会把堆栈中该实例之上的所有其他实例清除掉,这样一来该实例自然成为当前task堆栈中的栈顶实例,由它直接响应intent请求,如果此时被请求的Activity实例的启动属性是standard,那么连同当前堆栈中的这个同类实例会被Android系统一并清除,然后,Android系统创建一个新的被请求实例压入堆栈来应答intent请求。这是因为一旦被请求的Activity实例的启动属性被设定为standard的话,系统总是会新建一个实例压入堆栈来应答intent请求的。
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.
翻译:FLAG_ACTIVITY_CLEAR_TOP 和 FLAG_ACTIVITY_NEW_TASK常常是配合在一起使用的,它们俩个这样配合使用的真正目的是要求android系统从当前task以外的后台task堆栈中定位到一个已经存在的Activity实例,直接用该实例或是新建的该实例来响应intent。