What is a task?
What is the role of a task?
What is the relationship between task and stack?
When will a task be created?
A task is a set of activity instances. These instances are stored in the order of activation to complete a specific operation.
In a stack corresponding to a task, these instances can belong to one application or different application
For example, when writing a text message to add a contact, you need to open the activity in the address book, and send the text message from the text message to the add contact,
All the actions in this string belong to a task.
Android provides the task mechanism to improve user experience. For example, when you add a contact during text message writing, text message writing belongs to the SMS application,
The add contact list belongs to the contacts application. Although it belongs to two different applications, after the user adds the contact
Return to the text message writing interface and feel like an application. It is very smooth to use. This is the credit of the task.
When we click an icon in homescreen to start an application (this is assumed that the application is not running in the background ),
The first started activity creates a task, and each activity has an affinity attribute to specify which task the activity belongs,
By default, the affinity attribute of the activity in the application is the same, that is, the activity in an application belongs to the same task,
However, this is only the default situation. If you add attributes when the activity is registered in androidmanifest. XML
Android: taskaffinity = "com. Android. XXX" // The attribute name here cannot be the same as the activity package Android: allowtaskreparenting = "true"
When an activity is activated, another task is created, that is, an application can have multiple tasks. Although a new task is created,
However, when you press the return key, it will still return to the previous activity and read the official Google note:
Note:Although the activity starts in a new task,BackButton still returns the user to the previous activity.
The following two activity startup modes will also create tasks:
Singletask: this is the single task Startup Mode of the activity. When the activity is activated, the system first checks whether an instance already exists in an existing task,
If no instance exists, a new task is created and the activity is started in this task. If an existing instance exists, the task of the instance is taken as a whole.
Move from the background to the foreground. For details, see:
There are two situations: one is that the activity instance to be activated is at the top of the stack corresponding to the task; the other is that the activated activity instance is not in the task
The stack top of the corresponding stack. At this time, the instance is pushed to the top of the stack. All instances on this instance are pushed to the stack and are deregistered.
Singleinstance: this is the single instance Startup Mode of the activity. When this instance is created for the first time, a task exclusive to the instance is created,
There are no other instances in this task. If this task is in the background and then activated, the instance will be reused,
No new instances and tasks are created.