This reference to the official Dev Guide document, a brief Android under the affinities and tasks (task).
1, Activity and Task
task as if it can contain a lot of activity in the stack. By default, an activity start another activity, two activity is a task on the same stack, the second activity pressed into the first activity where the task stack. When the user presses the return key, the second pop-up activity from the stack, the first activity again in the current screen. Thus, from the user point of view, these two activity as if it belongs to the same application, even if the second activity is to belong to another application. Of course, this is the default. task stack contains the activity of the object. If an activity has multiple instances running, then the stack is stored in each instance of the entity. The activity will not be re-stack arrangement, only the pop-up and pressed into operation. A task all of the activity are in the form of a whole movement. The task can be moved to the foreground or background. Analogy, the current task contains four activity-current activity following a 3 activity. When the user press the HOME button to return to the application launcher (application launcher), select a new application (in fact, a new task), the current task was transferred to the background, the new task in the root activity will be displayed on the screen. Over time, the user presses the back key back to the application launcher screen, select the programs that run before (before task). That task, still contains four activity. When the user press the return key again, the screen does not display that left before the activity (the task before the root activity), which displays the current activity from the task out of the stack after stack of that activity. The behavior just described is the default activity and task behavior. There are many ways to change this behavior. the link between activity and task, and task activity in the behavior of the tag by intent and in the manifest of the <activity> element attribute control. Among the major Intent tags are:
The main <activity> attributes:
By default, an application all the activity has an affinity-that make them belong to the same task. However, each activity can be <activity> the taskAffinity property separate affinity. The activity of different applications can share the same affinity, with an application in the different activity can also set a different affinity. affinity properties work in two kinds of situations: when the start activity of Intent object contains FLAG_ACTIVITY_NEW_TASK tag, or when the activity of allowTaskReparenting is set to true.
l FLAG_ACTIVITY_NEW_TASK marked when passed to startActivity () of Intent object contains FLAG_ACTIVITY_NEW_TASK tag, the system will need to activate the activity to find the current activity of different task. If you want to start with the current activity of the affinity properties of the affinity of all the task attributes are not the same, the system will create a new affinity with the property that task, and to start the activity down to the new task stack; otherwise pressed into the activity affinity properties of the same stack.
l allowTaskReparenting property if an activity of allowTaskReparenting property is true, then it can be a task (TASK1) move to another with the same affinity in the task (TASK2) in (TASK2 into the foreground).
If a. Apk file from the user point of view contains a number of "application", you may need a different affinity to those values of Fu activity.
2, operation mode
activity of launchMode property can have four values:
l "standard" (default)
This four kinds of patterns can be divided into categories according to 4, the following assumptions in the activity1 task1 start activity2:
Mode \ Category inclusive activity2 the task
An activity is allowed to have multiple instances
activity is allowed to coexist with other activity in a task
For the new intent, whether the activity is always an instance of an object
If you do not include FLAG_ACTIVITY_NEW_TASK tag, activity2 into task1, otherwise described by the previous rules for the selection task activity2
Can be instantiated many times, with different instances of a task can be located in different task, each task may contain multiple instances allowed is. When receiving a new intent, we tend to generate a new activity object.
With the standard
With the standard
Allow existing activity object, if the target task at the top of the stack, then the activity is reused, if it is not in the top of the stack, it will instantiate the new activity object
Will activity2 into task1 bottom of the stack can not have multiple instances. As the mode activity is always in the top of the stack, so actvity in the same device at most only one instance where
Allowed. singleTask mode activity is always in the bottom of the stack location. Target activity instance already exists, if the instance is just the top of the stack in the task, then the receiver intent, or the arrival of intent will be discarded, but it can respond to the intent of the activity where the task will be moved to the front.
Not allowed to coexist with other activity in a task. If activity1 running in this mode, the activity2 certainly is in a different task with activity1
For the newly arrived intent, if the activity by the newly created object to receive, the user can return button to return to previous activity; if existing activity is to receive, the user can not go back to the receiver intent to return before state.
3, empty the stack when the user time to leave task (the current task was transferred to the background), the system will clear the task in the bottom of the stack all the activity outside the activity. Thus, when the user returns to the task, only to leave that task most of the initial activity.
This is the default situation, <activity> some properties can change this behavior.
l alwaysRetainTaskState property if the bottom of the stack activity of this attribute is set to true, just described will not happen. task all the activity will be stored for long periods.
l clearTaskOnLaunch property if the bottom of the stack activity of this attribute is set to true, once the user leave the task, the task stack activity will be left empty to the bottom of the stack activity. This is just the opposite alwaysRetainTaskState. Even if the user is temporarily away, task will be to return to the initial state (left bottom of the stack acitivty).
l finishOnTaskLaunch attribute this property and clearTaskOnLaunch similar, but operate only on the individual activity, rather than the entire task. It can end any activity, including the bottom of the stack activity. When it is set to true, the current activity only during the current session as part of task exist, when the user exits the activity and then return, it does not exist.
There is also a way to be able to stack the activity removed from the force. If the intent object contains FLAG_ACTIVITY_CLEAR_TOP mark, when the target task already exists in the object with the intent to receive the same type of activity instances of activity there, all the objects in the activity above the activity will be empty, so to receive the intent of the activity is located on the stack top, the object can respond to the arrival of intent. If the target activity of the operating mode for the standard, the target activtiy will be empty. Because when the operating mode for the standard, there will always create a new activity object the arrival of the intent to receive the object.
FLAG_ACTIVITY_CLEAR_TOP mark and FLAG_ACTIVITY_NEW_TASK often used together. With two markers can locate an existing activity, and it is in the position to respond to intent.
4, start the task (Task)
Intent filter in "android.intent.action.MAIN" action and "android.intent.category.LAUNCHER" category of activity will be marked as the entry task. Activity with these two tags will be displayed in the Application Launcher (application launcher) in the.
The second more important point is that users must be able to leave the task and then returned. For this reason, singleTask and singleInstance these two modes of operation can only be used with MAIN and LAUNCHER filter activity. Analogy, if not included with a MAIN and LAUNCHER filter, an activity model to run a singleTask activity, initiated a new task, when the user press the HOME key, that activity was the main screen "block ", and users can no longer return to that activity.
A similar situation may also occur in FLAG_ACTIVITY_NEW_TASK tag. If the tag will create a new task, press the HOME key when the user must have a way to allow users to return to that activity. Some things (such as notification manager) always require an external task to start activity, the intent in passing startActivity FLAG_ACTIVITY_NEW_TASK always included in tag.
For users that do not want to leave the situation after the return to activity can be finishOnTaskLaunch property to true.