Tasks and Back Stack
An app typically contains multiple activities, each activity performing different tasks.
A task is a collection of activities that perform a series of tasks interactively with the user, and these activities are arranged in a stack heap in order of opening.
The user taps the icon or shortcut on the main page, and if the app has never been executed, a new task is created, and the main interface activity acts as the root activity of the task, and when the root activity launches the other interface, A new activity is pushed to the top of the task stack and gets the focus. At this point the root activity is still saved in the task, and when the user presses the return key, the current activity is ejected from the task stack and destroyed, and the previous activity is re-resumes (the state data on the previous UI is reloaded).
The activities order in the task never reflow, only push (stack) and pop (out of the stack) two actions, as a stack, keep the principle of LIFO.
, 1 is the root of the task activity;1 start 2, 2 into the stack, 1 is pressed to the bottom of the stack, 2 on the top of the stack, 2 after the start of 3, 3 into the stack; the return key, 3 out of the stack is destroyed, 2 continues at the top of the stack. All activity at the top of this task is visible and gets the user's focus interface.
When all the activities are out of the stack, then the task does not exist.
The task can be used as a whole unit and can be moved to the background as a whole, when the user returns to the main interface via the home key and launches a new task.
Note: Multiple tasks can be in the background at the same time, but may be destroyed by the system when the memory is reclaimed.
The default behavior for activities and tasks is as follows:
- A boot B,a is stopped and the system holds the status data for a. If the user presses the back key in the B interface, B destroys, a restores the saved state data.
- The user presses the home key to leave all activities stops in a task,task and returns to the background, and if the user then launches the task through the desktop icon, the task will be cut to the foreground and resumed on top of the stack activity.
- If the user presses the return key, the activity at the top of the current stack is stacked and destroyed, and the next activity that is cut to the top of the stack is resumed. When the activity is destroyed, the system does not save its state data.
- Activities can be instantiated multiple times, even in different tasks.
These activities interface user temporary data is lost when the system destroys activities in the back-end task due to memory reclamation (even so, the order of the tasks still exists), when these activities back to the top of the stack, not through resume, But through recreate. To avoid such data loss, it is strongly recommended to save temporary data through Onsaveinstancestate ().
Manage tasks
Sometimes you need an activity to recreate a new task each time it is started, rather than to an existing task, or when you create an activity, use an activity instance that you already created directly, instead of creating a new instance at the top of the stack Or you want the user to clear the activities from the root activity after leaving the task.
You can change the default behavior by <activity> 's properties and intent flag.
The <activity> properties are as follows:
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch
Intent's flag is as follows:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
Caution: Most applications do not need to change the default behavior of the system, and if you decide that you need to change the default behavior, then you need to ensure that the availability of the application in the startup, return and other scenarios, there is a possibility that the return key behavior will conflict with the user's expectations.
Defining the startup mode
The startup mode allows you to customize the association of a new activity instance to the current task in the following two ways:
- With manifest file: How this activity is associated when it is started
- Use intent Flags: how the newly initiated activity should be associated
If a start-up b,b can be defined by manifest, a can also define the association of B by intent flags, and if two are defined, then A's intent flags take precedence.
Note: The two methods are not equivalent, some patterns can only be defined with the manifest file, and some can only be defined with intent flags.
Using the manifest file
The Launchmode attribute of the <activity> element:
- Standard (default mode)
- The default mode, the system creates a new activity instance, the activity can be instantiated several times, each instance can belong to different tasks, a task can contain multiple instances.
- Singletop
- If an instance of the activity already exists and is at the top of the task's stack, the system invokes the instance
onNewIntent()
instead of creating a new instance. Activity can be instantiated multiple times, each instance can belong to a different task, and a task can have multiple instances (the task at the top of the stack is not an instance of this activity)
- For example, A-b-c-d,a is the bottom of the stack, D is the top of the stack, and then a intent need to start D, if D is standard mode, then it becomes a-b-c-d-d, then if D is singletop mode, then a-b-c-d is still Intent will be received by the onnewintent of D. If you start B at this point, then no matter what mode B is, it's a-b-c-d-b.
- Singletask
- The system creates a new task, however, if an instance of the activity already exists in another task, the system will distribute the intent to the existing instance, triggering its onnewintent () instead of creating a new instance, the system can only have one instance, and so is the browser.
- SingleInstance
- As with Singletask, this activity is the only member of a task except that the system cannot start other activites to the task in which the activity exists.
In general, the return key always brings the user to the previous interface, and then when you start activity in Singletask mode, if the activity instance already exists in the background task, then the entire task is cut to the foreground, The return key is a display of all the activities in a task that has just been cut into the foreground. As shown in the following:
The Launchmode attribute in Note:manifest may be overridden by intent flags.
Using Intent Flags
Flag_activity_new_task
Consistent with Singletask
Flag_activity_single_top
Consistent with Singletop
Flag_activity_clear_top
If the activity instance is already in the current task and is started, the activities on top of this instance is destroyed, and the instance Fires Onnewintent () and is on top of the stack.
Flag_activity_clear_top and Flag_activity_new_task are commonly used in conjunction.
Note: If launch mode is standard, an existing instance is also destroyed, replacing the location where the instance was destroyed by creating a new instance, because the standard mode always receives intent by creating a new instance.
Handling affinities
Affinity means which task the activity belongs to. By default, all activities for the same app have affinity for each other. So by default, all activities of the same app are in one task. Then, you can also modify the default affinity. Activities in different apps can share a affinity, or activities in the same app can define different affinities.
Affinity,taskaffinity is the only string that can be modified by the Taskaffinity property of <activity>.
Two scenarios for using affinity:
- Intent contains Flag_activity_new_task, the general system creates a new task, and if the taskaffinity of the existing task is the same as the newly initiated ACTIVITY, no new task is created.
- When the Allowtaskreparenting property is true,
Tip: If the apk contains more than one "application", then you can define taskaffinity to differentiate.
Clearing the back stack
When a user leaves a task for a long time, the system cleans up all activities outside of the root activity, and when the user returns to those tasks again, only the root activity is displayed.
Users can modify this default behavior in the following ways:
Alwaysretaintaskstate
If true in root activity, then the above default behavior will not occur, and all activities in this task will persist for a long time.
Cleartaskonlaunch
If true in root activity, then the above default behavior occurs immediately after the user leaves the task
Finishontasklaunch
Similar to Cleartaskonlaunch, but only for an activity, not an entire task. Root activity is also cleared if the defined property is true.
Start a task
同时定义"android.intent.action.MAIN"
and "android.intent.category.LAUNCHER"的activity
a portal icon on the desktop.
This is important: the user can either leave this task or return to the task through the desktop icon, so defining these two filiter activites typically initializes a task.
Singletask and singleinstance should be defined on this activity, imagine, singletask initiated activity in a new task, when the user presses the home key, if there is no portal icon on the desktop, Then this task will never be returned.
If you really don't want users to be able to return an activity, you can set the Finishontasklaunch property.