Android tasks and returned stacks are completely parsed, detailing the details you don't know, and android details
Reprinted please indicate the source: http://blog.csdn.net/guolin_blog/article/details/41087993
The main content of this article is Android Doc. I have done some processing after translation, and good English friends can directly read the original text.
Http://developer.android.com/guide/components/tasks-and-back-stack.html
Task and return stack
An application usually contains many activities. Each Activity should be designed as a component with specific functions and users can perform operations. In addition, activities should be able to start each other. For example, an email application may contain an Activity used to display the email list. When a user clicks an email, another Activity is opened to display the specific content of the email.
In addition, an Activity can even start the Activity in other applications. For example, if your application wants to send an email, you can define an Intent with the "send" action and input some data, such as the recipient's email address and email content. In this way, if an Activity declaration in another application can respond to the Intent, the Activity will be opened. In the current scenario, this Intent is used to send emails. Therefore, the email Activity in the email application should be opened. After the email is sent, it will still return to your application, which makes the user look like the Activity that just wrote the email is part of your application. Therefore, even if many activities come from different applications, the Android system can still integrate them seamlessly, this is because these activities exist in the same Task.
A task is a collection of activities. It uses stacks to manage the activities. This stack is also called back stack ), the Activity sequence in the stack is stored in sequence according to the order in which they are opened.
The Home page of the mobile phone is where most tasks start. When you click an application icon on the Home page, the task of this application will be transferred to the foreground. If this application does not have any task (indicating that the application has not been started recently), the system will create a new task, and put the main Activity of the application into the return stack.
When an Activity starts another Activity, the new Activity will be placed on the top of the stack that returns the stack and the focus will be obtained. The previous Activity is retained in the returned stack but will be stopped. When you press the Back key, the Activity at the top of the stack will be removed, and the previous Activity will have to return to the top position. The order of the returned Activity in the stack will never change. We can only add the Activity to the top of the stack or remove the Activity from the top of the stack. Therefore, the return stack is a typical data structure of last in, first out. The timeline clearly shows the status changes of multiple activities in the returned Stack:
If you press the Back key all the time, the activities in the returned stack will be removed one by one until they are finally returned to the main screen. When all the activities in the returned Stack are removed, the corresponding task does not exist.
In addition to being transferred to the foreground, tasks can also be transferred to the background. When you start a new task or click Home to return to the Home screen, the previous task will be transferred to the background. When the task is in the background state, all the activities returned to the stack will be stopped, but the order of these activities in the stack will remain intact, as shown in:
At this time, you can switch any background task to the foreground, so that you can see the Activity at the top when you left the task. For example, there are three activities in the stack of task A. Now you press the Home Key and click the icon on the desktop to start another application. When the system returns to the desktop, task A has already entered the background, and when another application is started, the system starts a new task (Task B) for this program ). After you use this program, press the Home key again to return to the desktop. At this time, Task B enters the background. Then, the user re-opened the program used for the first time. At this time, task A will return to the front-end, and the three activities in task stack A will still keep the previous order, the top Activity changes to the running status. You can still switch back to Task B by using the Home or multitasking keys, or start more tasks. This is an example of multi-task switching in Android.
Because the order of the returned Activity in the stack will never change, if your application allows multiple entries to start the same Activity, then, a new instance of the Activity will be created each time it is started, instead of moving the following Activity to the top of the stack. This will easily lead to a problem, that is, the same Activity may be instantiated many times, as shown in:
However, if you do not want the same Activity to be instantiated multiple times, it is also possible. We will immediately discuss how to implement this function, now let's briefly summarize the default task and Activity behavior:
- When Activity A starts Activity B, Activity A enters the stop state, but the system retains all its related information, such as the scroll position and the content entered in the text box. If you press the Back key in Activity B, Activity A returns to the running status.
- When you exit a task by using the Home key, the task enters the background, and all the activities returned to the stack are in the stopped status. The system will keep the status of these activities, so that when you re-open the application next time, you can directly extract background tasks to the foreground, and restore the previous top Activity.
- When you press the Back key, the current top Activity will be removed from the return stack, and the removed Activity will be destroyed, then, the previous Activity is at the top of the stack and enters the active state. When an Activity is destroyed, the system will not retain any status information for it.
- Each Activity can be instantiated many times, even in different tasks.
Manage tasks
The Android system manages tasks and returns stack methods. As described above, all started activities are put into the same task, it is managed through a "first-in-first-out" stack. In most cases, this method is okay, and developers do not have to worry about how the Activity in the task is stored in the returned stack. However, if you want to break this default behavior, for example, when starting a new Activity, you want it to exist in an independent task instead of an existing task. Or, when starting an Activity, if the Activity already exists in the returned stack, you want to move the Activity directly to the top of the stack instead of creating another instance. Alternatively, you want to clear all the other activities except the Activity at the bottom of the stack. These functions can even be implemented by setting the attribute of the <activity> element in the manifest file or configuring the flag of Intent when the Activity is started.
In the <activity> element, the following attributes can be used:
- TaskAffinity
- LaunchMode
- AllowTaskReparenting
- ClearTaskOnLaunch
- AlwaysRetainTaskState
- FinishOnTaskLaunch
Among Intent, the following flags are commonly used:
- FLAG_ACTIVITY_NEW_TASK
- FLAG_ACTIVITY_CLEAR_TOP
- FLAG_ACTIVITY_SINGLE_TOP
Next we will discuss how to change the default behavior of an Activity in a task through the manifest parameter and Intent flag.
Define Startup Mode
The start mode allows you to define how to associate an Activity instance with the current task. You can define the start mode in two different ways:
1. Use the manifest File
When you declare an Activity in the manifest file, you can specify how the Activity is associated with the task at startup.
2. Use Intent flag
When you call the startActivity () method, you can add a flag to the Intent to specify how the newly started Activity is associated with the current task.
That is to say, if Activity A starts Activity B, Activity B can define how it is associated with the current task, and Activity A can also require Activity B to associate with the current task. If Activity B has defined in manifest how to associate with the task, and Activity A also requires that Activity B be associated with the current task in Intent, in this case, the definition in Intent will overwrite the definition in manifest.
Note that some startup modes can be specified in manifest, but cannot be specified in Intent. Similarly, some startup modes can be specified in Intent, but cannot be specified in manifest. Let's discuss them in detail.
Use the manifest File
When defining an Activity in the manifest file, you can use the launchMode attribute of the <activity> element to specify how the Activity is associated with the task. The launchMode attribute has the following four optional parameters:
"Standard" (default startup mode)
Standard is the default start mode, that is, if the launchMode attribute is not specified, this start mode is automatically used. This mode indicates that the system creates a new instance every time the Activity is started, and it is always put into the current task. The Activity in this startup mode can be instantiated multiple times. A task can also contain multiple instances of this Activity.
"SingleTop"
This startup mode indicates that if the Activity to be started already exists in the current task and is still at the top of the stack, the system will not create another instance for the Activity, instead, the onNewIntent () method of the stack top Activity is called. The Activity in this startup mode can also be instantiated multiple times. A task can also contain multiple instances of such Activity.
For example, the returned stack of A task contains Activity A, Activity B, Activity C, and Activity D. Activity A is at the bottom and Activity D is at the top. At this time, if we want to restart D again, and the Startup Mode of D is "standard", then the system will create another D instance and put it into the return stack, the element in the stack is A-B-C-D. If the Startup Mode of D is "singleTop", because D is already at the top of the stack, the system will not create another D instance, instead, the onNewIntent () method of D Activity is called directly. The element in the stack is still A-B-C-D.
"SingleTask"
In this startup mode, the system creates a new task and places the started Activity to the bottom of the stack of the new task. However, if an instance of the Activity already exists in an existing task, the system will not create its instance again, but will directly call its onNewIntent () method. Declare the Activity in this startup mode. Only one instance exists in the same task. Note that all the activities we call start here refer to the activities in other applications, because the "singleTask" Mode creates a new task only when the Activity of other programs is started by default, and the same task will be used to start the Activity in your own program. The specific causes are as follows:Processing affinity.
"SingleInstance"
This startup mode is similar to singleTask, except that the system does not add other activities to the task where the Activity declared as "singleInstance" is located. That is to say, there is always only one Activity in the task where the Activity is located, and other activities opened through this Activity will also be put into other tasks.
For another example, the built-in browser program of the Android system declares that the Activity of browsing the webpage should always be opened in an independent task, this is achieved by setting the "singleTask" Startup Mode in the <activity> element. This means that when your program is ready to open the Android built-in browser, the newly opened Activity will not be put into your current task, but will start a new task. If the browser program already has a task in the background, the task will be switched to the foreground.
In fact, whether the Activity is started in a new task or in the current task, the Return key will always bring us back to the previous Activity. However, there is a special case, that is, if the Activity specifies the start mode as "singleTask" and starts the Activity in another application, when this Activity is found to be in a background task, the whole background task is directly switched to the foreground. At this time, pressing the return key will give priority to the current foreground tasks (just switched from the background to the foreground) for rollback. This situation is visually displayed: