Affinities and tasks in Android)

Source: Internet
Author: User
This article introduces affinities and tasks in Android by referring to the official Dev guide document ). Activity and task

A task is like a stack that can contain many activities. By default, when an activity starts another activity, the two activities are placed in the same task stack, and the second activity is pushed to the task stack where the first activity is located. When you press the return key, the second activity pops up from the stack, and the first activity is displayed on the current screen. In this way, from the user's point of view, these two activities belong to the same application, even if the second activity belongs to another application. Of course, this refers to the default situation. The task stack contains activity objects. If an activity has multiple instances running, the stack stores the entity of each instance. The activities in the stack are not re-arranged, and only pop-up and push-in operations are performed. All the activities in a task are moved as a whole. The entire task can be moved to the foreground or background. For example, the current task contains four activities-the current activity has three activities. After you press the Home Key to return to the application launcher, select a new application (in fact a new task) and the current task is transferred to the background, the root activity in the new task will be displayed on the screen. After a while, you press the return key to return to the program starter interface and select the program (previous task) that was run before ). The task still contains four activities. When you press the return key again, the screen does not display the previous activity (the root activity of the previous task), but shows the activity of the current activity removed from the task stack and at the top of the stack. The behavior just described is the default activity and task behavior. There are many ways to change this behavior. The relationship between the activity and the task, and the activity behavior in the task can be controlled by the intent tag and the attribute of the <activity> element in the manifest. The main intent labels are as follows:

  • Flag_activity_new_task
  • Flag_activity_clear_top
  • Flag_activity_reset_task_if_needed
  • Flag_activity_single_top
The main <activity> attributes include:
  • Taskaffinity
  • Launchmode
  • Allowtaskreparenting
  • Cleartaskonlaunch
  • Alwaysretaintaskstate
  • Finishontasklaunch

By default, all activities in an application have an affinity-which makes them attribute the same task. However, each activity can set a separate affinity through the taskaffinity attribute in <activity>. The activity in different applications can share the same affinity, and the activity in the same application can be set to different affinity. The affinity attribute determines that the intent object to start the activity must contain the flag_activity_new_task tag. The allowtaskreparenting of the activity is considered to be set to true.

Flag_activity_new_task tag

When the intent object passed to startactivity () contains the flag_activity_new_task tag, the system searches for different tasks for the activity to be started. If the affinity attribute of the activity to be started is different from that of all current tasks, the system creates a new task with the affinity attribute, and press the activity to be started into the new task stack; otherwise, press the activity into the stack with the same affinity attribute.

Allowtaskreparenting attribute

If the allowtaskreparenting attribute of an activity is true, it can be moved from one task (task1) to another task (task2) with the same affinity (when task2 is taken to the foreground ).

If a .apk file contains multiple "Applications" from the user's perspective, you may need to assign different affinity values to those activities.

Running Mode

The launchmode attribute of an activity can have four values:

  • "standard"(Default)
  • "singleTop"
  • "singleTask"
  • "singleInstance"

The four modes can be distinguished by four categories. The following is assumed that activity1 in Task 1 starts activity2:

Mode \ category Activity2-compatible tasks Whether an activity can have multiple instances Whether other activities can coexist in one task Whether the new intent always instantiates the activity object
Standard If the flag_activity_new_task tag is not included, activity2 is placed in Task 1. Otherwise, select Task for activity2 according to the rules described earlier. It can be instantiated multiple times. different instances of the same task can be located 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 as standard Same as standard Allow An existing activity object. If it is located at the top of the stack of the target task, the activity is reused. If it is not located at the top of the stack, a new activity object is created.
Singletask Put activity2 at the bottom of task1 Stack You cannot have multiple instances. In this mode, the activity is always at the top of the stack. Therefore, actvity has only one instance on the same device. Yes. The activity in singletask mode is always located at the bottom of the stack. When the target activity instance already exists, if the instance is located at the top of the task stack, the intent will be received; otherwise, the arrival intent will be discarded, however, this causes the task of the target activity to be moved to the foreground.
Singleinstance Same as singletask Same as singletask It is not allowed to coexist with other activities in one task. If activity1 runs in this mode, activity2 and activity1 must be in different tasks.

For a new intent, if it is received by a newly created activity object, the user can return to the previous activity through the return key; if it is received by an existing activity, the user cannot return the status before receiving the intent through the return key.

Clear Stack

When a user leaves the task for a long time (the current task is transferred to the background), the system will clear all the activities outside the stack activity in the task. In this way, when the user returns to the task, only the initial activity of the task is left.

This is the default situation. Some attributes in <activity> can change this behavior.

Alwaysretaintaskstate attribute

If the attribute of the activity at the bottom of the stack is set to true, the situation described above will not happen. All activities in the task will be saved for a long time.

Cleartaskonlaunch attributes

If the attribute of the activity at the bottom of the stack is set to true, once the user leaves the task, the activity in the task stack will be cleared to only the activity at the bottom of the stack. This is exactly the opposite of alwaysretaintaskstate. Even if the user leaves for a short time, the task will return to the initial state (only the stack bottom acitivty is left ).

Finishontasklaunch attributes

This attribute is similar to cleartaskonlaunch, but it only operates on individual activities, rather than the entire task. It can end any activity, including the activity at the bottom of the stack. When it is set to true, the current activity only exists as a part of the task during the current session. When the user exits the activity and returns the result, it will not exist.

Another method is to forcibly remove the activity from the stack. If the intent object contains the flag_activity_clear_top tag, if the target task already has an activity instance of the same type as the activity that receives the intent object, all the activities located above the activity object will be cleared, so that the activities that receive the intent will be located at the top of the stack and can respond to the arrival of the intent object. If the target activity runs in standard mode, the target activtiy is also cleared. Because when the running mode is standard, a new activity object is always created to receive the arrival intent object.

Flag_activity_clear_top mark is often used with flag_activity_new_task. You can use two tags to locate an existing activity and place it in a position that can respond to the intent.

Start a task)

Intent filter has"android.intent.action.MAIN"Action and"android.intent.category.LAUNCHER"CATEGORY activity will be marked as the task entry. The activity with these two tags will be displayed in application launcher.

The second important point is that the user must be able to leave the task and return it later. For this reason, singletask and singleinstance run modes can only be applied to activities containing the main and launcher filters. For example, if the main and launcher filters are not included, an activity runs a singletask activity and initializes a new task. When you press the Home key, the activity is blocked by the main screen, and the user can no longer return to the activity.

Similar situations also occur in the flag_activity_new_task mark. If this tag creates a new task, when you press the Home key, there must be a way for the user to return to that activity. Some things (such as notification manager) always require that the activity be started in an external task. The intent passed to startactivity always contains the flag_activity_new_task tag.

You can set the finishontasklaunch attribute to true if you do not want the user to return the activity after leaving.

Related Article

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.