Reproduced Android under Affinities and task

Source: Internet
Author: User

SOURCE Links: http://appmem.com/archives/4051, activity, and task

Task is like a stack that can contain a lot of activity. By default, when an activity initiates another activity, two activity is placed in the same task stack, and the second activity presses into the task stack where the first activity is located. When the user presses the return key, the second activity pops out of the stack, and the first activity is displayed on the current screen. Thus, from a user's point of view, the two activity is as if it belonged to the same application, even though the second activity belongs to another application. Of course, this refers to the default situation. The task stack contains objects that are activity. If an activity has multiple instances running, then the stack holds the entity for each instance. The activity in the stack is not rearranged, only the eject and press-in operations. All the activity in a task is moved in the form of a whole. The entire task can be moved to the foreground or background. For example, the current task contains 4 activity– The current activity has 3 activity below. When the user presses the home key to return to the Program Launcher (Application Launcher), selects a new application (in fact a new task), the current task is moved to the background, and the root activity in the new task is displayed on the screen. After a while, the user presses the back key back to the Program Launcher interface and selects the previously running program (the previous task). That task, which still contains 4 of activity. When the user presses the return key again, the screen does not display the activity that was left before (the root activity of the previous task), and the activity that the current activity moved out of the top of the stack from the task stack. The behavior that you just described is the default activity and task behavior. There are many ways to change this behavior. The connection between activity and task, as well as the behavior of activity in a task, can be controlled by the markup in intent and the properties of the <activity> element in manifest. Among them, the main intent tags are:

L Flag_activity_new_task

L Flag_activity_clear_top

L flag_activity_reset_task_if_needed

L Flag_activity_single_top

The main <activity> properties are:

L Taskaffinity

L Launchmode

L allowtaskreparenting

L Cleartaskonlaunch

L Alwaysretaintaskstate

L Finishontasklaunch

By default, all activity in an application has a affinity– which makes them belong to the same task. However, each activity can be set up with a separate affinity through the Taskaffinity property in <activity>. Activity in different applications can share the same affinity, and different activity in the same application can be set to a different affinity. The Affinity property works in 2 situations: When the intent object that initiates the activity contains the flag_activity_new_task tag, or when the activity's allowtaskreparenting is set to true.

L flag_activity_new_task Mark

When the intent object passed to StartActivity () contains a flag_activity_new_task tag, the system will look for a task that is different from the current activity for the activity that needs to be started. If the affinity property of the activity to be started is not the same as the Affinity property of all the current tasks, the system creates a new task with that affinity property and presses the activity to start into the new task stack Otherwise, the activity is pressed into the same stack as the Affinity property.

L allowtaskreparenting Property

If the Allowtaskreparenting property of an activity is true, it can be moved from one task (TASK1) to another task (TASK2) with the same affinity (TASK2 to the foreground).

If an. apk file contains multiple "Applications" from the user's point of view, you may need to assign different affinity values to those activity.

2. Operation mode

The Launchmode property of the activity can have four values:

L "standard" (default)

L "Singletop"

L "Singletask"

L "SingleInstance"

These 4 modes can be differentiated by 4 classifications, the following assumes the activity1 start Activity2 in Task1:

mode \ category

inclusive Activity2 the Task

an activity whether multiple instances are allowed

Activity is there any other activity allowed ? coexist in a task

for the new intent , whether the activity is always instantiated Object

Standard

If the flag_activity_new_task tag is not included, Activity2 is placed in Task1, otherwise the task is selected for Activity2 as described previously

can be instantiated multiple times, different instances of the same task can be in different tasks, and each task can contain multiple instances

Allow

Yes. When a new intent is received, a new activity object is always generated.

Singletop

Same standard

Same standard

Allow

An existing activity object, if it is at the top of the stack of the target task, is reused, and if it is not at the top of the stack, the new activity object is instantiated

Singletask

Put the activity2 on top of the TASK1 stack

Cannot have more than one instance. Since the activity is always at the top of the stack in this mode, Actvity has at most one instance in the same device

Allow. Singletask mode activity is always located at the bottom of the stack. When the target activity instance already exists, if the instance is just at the top of the task stack, it receives intent, otherwise the incoming intent will be discarded, but the task that can be intent should be moved to the foreground.

SingleInstance

With Singletask

With Singletask

Does not allow coexistence with other activity in a task. If the activity1 is running in this mode, then Activity2 must be in a different task than activity1

For the new intent, if it is received by the newly created activity object, the user can return to the previous activity by returning the key, and if it is received by an existing activity, the user cannot return to the state before receiving intent by the return key.

3. Empty stack

When a user leaves a task for a long time (the current task is moved to the background), all activity outside the bottom of the task is purged. This way, when the user returns to the task, only the task's initial activity is left.

This is the default condition,<activity> Some properties can change this behavior.

L Alwaysretaintaskstate Property

If this property of the bottom activity is set to true, the situation just described will not occur. All activity in the task is saved for a long time.

L Cleartaskonlaunch Property

If this property of the bottom activity is set to true, once the user leaves the task, the activity in the task stack is emptied to only the bottom of the stack activity. This is just the opposite of alwaysretaintaskstate. The task returns to its initial state (only the bottom of the stack acitivty), even if the user is only momentarily away.

L Finishontasklaunch Property

This property is similar to Cleartaskonlaunch, but it is only for individual activity operations, not for the entire task. It can end any activity, including activity at the bottom of the stack. When it is set to true, the current activity only exists as part of the task during the current session, and when the user exits the activity and returns, it does not exist.

There is also a way to forcibly remove activity from the stack. If the intent object contains a flag_activity_clear_top tag, when an activity instance with the same activity type that received the intent object already exists in the target task, All activity on the activity object is emptied so that the activity that receives the intent is at the top of the stack and responds to the incoming intent object. If the target activity is running in standard mode, the target activtiy will also be emptied. Because when run mode is standard, a new activity object is always created to receive the incoming intent object.

Flag_activity_clear_top tags are often used in conjunction with Flag_activity_new_task. Use 2 markers to locate an existing activity and leave it in a position that responds to intent.

4. Startup Task (Task)

Intent filter with "Android.intent.action.MAIN" Action and "Android.intent.category.LAUNCHER" The activity of category will be marked as the entry of the task. Activity with these two tags will be displayed in the Application Launcher (Application Launcher).

The second important point is that the user must be able to leave the task and return later. For this reason, both the singletask and singleinstance operating modes can only be applied to activity that contains the main and launcher filters. For example, if you do not include a filter with main and launcher, an activity that runs a singletask-mode activity Initializes a new task, and when the user presses the home key, the activity is "blocked" by the main screen. , the user can no longer return to that activity.

A similar situation will appear on the flag_activity_new_task tag. If the tag creates a new task, there must be a way for the user to return to that activity when the user presses the home key. Some things (such as notification Manager) always require that ACTIVITY be started in an external task, and Flag_activity_new_task tags are always included in the intent passed to StartActivity.

Set the Finishontasklaunch property to true for situations where you do not want the user to leave and then return to activity.

Reproduced Android under Affinities and task

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.