activity任务堆栈(Activitytaskstack)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

activity任务堆栈(Activity task stack)
活动和任务
1、活动用任务来组成堆栈堆栈里可以有别的应用里的活动。

2、用户感知一个应用不是看开发包(.apk文件),是看任务堆栈。

一个任务堆栈对用户来说就是一个应用。

3、任务堆栈只对活动做入栈,弹出操作。

4、任务堆栈只能设置一个值:偏好。

5、任务堆栈中的活动是作为一个整体停留在前台或者后台。

6、任务堆栈和其中的活动的行为可以用启动该活动的意图的标志位和AndroidManifest. xml文件里<活动>的属性信息来控制。

亲和度和新任务
1、应用程序里的每一个活动都有自己的偏好,默认情况下,应用程序里的活动有一样的偏好,他们在一个任务堆栈里。

可以通过<活动>标签的taskAffinity属性来设置偏好。

一个应用程序中可以设置不同的偏好。

偏好也可以跨应用程序,不同的应用程序可以用同一个偏好。

偏好在两种环境下生效:意图使用flag_activity_new_task标志位,或者活动的allowTaskReparenting(同意任务重排根目录)属性设置成真实。

2、默认的情况:startactivity()启动的新活动和调用startactivity()方法的活动在一个任务堆栈中。

3、本flag_activity_new_task旗
在默认情况下,startactivity()总是和调用者在一个任务堆栈里,但是如果使用了flag_activity_new_task标志就会在不同的任务堆栈中(发现在一个APK里加上flag_activity_new_task也会在一个堆栈里,不管要启动的活动是不是设置了不同的偏好)。

但是如果已经有一个和将要运行的活动偏好相同的堆栈,就会运行在那个堆栈上,不开辟新堆栈。

4、的allowTaskReparenting属性
如果allowTaskReparenting属性设置真实。

这个活动就可以从它最初开始的堆栈,移动到和它偏好相同的堆栈,当和它偏好相同的堆栈的到前台的时候。

比如一个旅游程序里有一个天气预报的活动,在另外一个程序里打开了这个活动,当旅游程序被调到前台的时候,那么这个天气预报活动显示在前台。

注意:如果一个.apk文件的程序里有多个从用户角度看起来像单独
应用的观点,应该给每一个观点包含的活动设置不同的偏好。

发射方式
在运行模式在<活动>的launchMode属性设置。

可以设置成:
“标准”(标准模式,默认,“标准”模式总是构造一个新的实例来响应新的意图。

见清理堆栈章节最后部分)
“singleTop”(独享堆栈顶端)
“singleTask”(独享任务堆栈)
“singleInstance”(单例)
从4点区分这4种模式:
1)什么样的任务堆栈接受要启动的活动。

在标准和singleTop模式,除非是使用flag_activity_new_task标志位,否则要启动的活动总是在启动它们的堆栈中。

singleTask和singleInstance模式一旦存在了一个堆栈中,就不再运行在其他堆栈。

2)是否可以有多个实例。

标准和singleTop模式,可以有多个实例,可以多个实例分属多个任务堆栈,也可以在一个任务堆栈中有多个实例。

There is only one instance of singleTask and singleInstance schema.
3) do you agree that there are other instances of activity in their task stack?
The singleInstance pattern keeps only one activity of its own in its task stack. If it starts other activity, that activity is in the other stack, whether or not the
FLAG_ACTIVITY_NEW_TASK flag is added. In other ways, singleInstance and singleTask are the same.
The other three patterns allow multiple activity in their own stack. The singleTask pattern always maintains that it is the root activity of the task stack (bottom, but doesn't verify that it's always at the bottom).
4) whether a new instance will be generated when Intent is started.
The Standard pattern generates a new instance every time. Each instance corresponds to a Intent.
SingleTop mode: if the activity is to be started at the top of the target stack, it is not instantiated to use the existing activity (but this will trigger the onNewIntent (Intent intent) event). If not at the top, then a new instantiation is placed at the top of the stack.
The "singleTask" and "singleInstance" patterns do not generate new instances to handle Intent. The "singleInstance" pattern has always been in its own Task stack, so it has been at the top of the stack, and it has been able to respond to the new Intent. But the singleTask pattern allows other activity to be in its stack, and if it's at the top of the stack, he can respond to the new Intent. If it is not at the top of the stack, then the new Intent will fail to call, although the call will fail, but the stack in which the singleTask mode is still will be transferred to the front desk.
Note: 1. when the existing activity responds to the new Intent, the new activity is not instantiated, but the existing activity onNewIntent () method is called. To retrieve the Intent that originally started it, you can use getIntent ()
Method。

2. if there is no instantiation of a new activity, the point back key will change the state of multiple task stacks. For example, A-B-C, C started the D, shared a D with another stack E-F-D, returned the key by point, ended D, and became A-B-C and E-F.
Clearing the stack
If the user leaves a task stack for a long time, the system automatically removes all the activity except root activity. When the user returns, just like the user himself does not use the task stack, only the initial activity appears. The idea is: after a long time, the user will forget what he did before opening the task stack, and then go back and start doing something else. This is the default behavior, but there are also some attributes that change the default behavior.
AlwaysRetainTaskState attribute:
If a task stack is set with activity, the attribute is TRUE, then all the activity in this task stack will be retained and will not be cleared automatically after a long time.
ClearTaskOnLaunch attribute:
And the alwaysRetainTaskState attribute instead, if this property is set to TRUE, not the task of maintaining the stack, whenever a user clicks on the program, will the task stack all empty and start again (after verification with activity will be empty, empty after the operation of the new task stack ID unchanged.)
FinishOnTaskLaunch attribute:
Just like clearTaskOnLaunch, but this property is on a single activity, not on the entire task stack.
When the task stack is running again, the finishOnTaskLaunch set to TRUE of activity will be cleared, and the other activity will remain unchanged.
Another way to forcibly remove activity is to add a
FLAG_ACTIVITY_CLEAR_TOP flag to the Intent, where the target stack will be started if Intent exists, activity,
Then the activity of the target stack above this activity will be cleared. If the activity mode is "standard", it itself will be cleared, and then a new instance of this activity will be re constructed to respond to Intent. Because the "standard" pattern always constructs a new instance to respond to the new intent.
Starting tasks
A activity uses the Intent Filters of
android.intent.action.MAIN and
UNCHER to indicate that it is the entry of a task. In this way, the program manager creates the icon for this task. This represents 2 points: 1, the user can run the task 2 through it, and after a period of time, the user can return it.
What's important is second points. The user must be able to leave and return the task. Therefore, singleInstance and singleTask should be used together with MAIN and LAUNCHER filters, because the activity of these two modes always initializes a task. Suppose, if there is no MAIN, LAUNCHER filter, also used the singleTask model activity to start a new task, the user something in this task, and then click home button, the task now was transferred to the backstage to see. But it can't start this task in the program manager, and the user can't get back to the task.
The same thing that needs to be taken care of is the
FLAG_ACTIVITY_NEW_TASK logo. Using this flag opens a new task. When the user point home leaves, there is a need to ensure that there is a direction that allows the user to get back to the task. Some entities always start new tasks, such as notification manager. Notification manager always adds a FLAG_ACTIVITY_NEW_TASK flag to boot activity. If you have a activity that uses this flag area to call an external task, notice that the user has an independent way to return to this task.
You don't want the user to start a program when you see a activity, and you can use the finishOnTaskLaunch flag.。

相关文档
最新文档