Android task and rollback stack-Overview

Source: Internet
Author: User

An application usually contains multiple activities. Each activity should be designed around the specific action types that a user can execute and be able to start other activities. For example, an email application may have an activity used to display the new email list. When you select an email, there will be a new activity used to open the email.

An activity can even start the activity that exists in other applications on the device. For example, if your application wants to send an email, you can define an intent that performs the "send" action and contain some data, such as the email address and content. Then open the Declaration from another application to process this intent type activity. In this scenario, intent sends an email, so the "write" activity of an email application is started (if multiple activities support the same intent, then the system will allow the user to select one of them ). After an email is sent, your activity is restored, and the activity of this email is like a part of your application. Even if these activities come from different applications, android can keep these activities in the same task to provide users with a seamless user experience.

A task is a set of activities that interact with users when a job is executed. These activities are arranged in a stack in the order of opening (rollback stack ).

The home screen of a device is the start position of most tasks. When you touch the icon in an application launcher (or a shortcut icon on the home screen), the application tasks are displayed on the foreground. If the task of the relevant application does not exist (the corresponding application has not been used recently), a new task will be created, the "Main" activity opened by the application will act as the root activity of the task.

When another activity is started for the current activity, the new activity is placed at the top of the stack with focus. The previous activity remains in the stack but ends. When an activity is terminated, the current status of the user interface is retained. When you press the rollback button, the current activity pops up from the top of the stack (this activity is destroyed), and the previous activity is restored (the previous UI is saved ). The activity in the stack will not be rearranged. It will only be pushed to and the pop-up stack. The activity will be pushed to the top of the stack when the current activity starts. The activity will pop up from the stack when the rollback button is used to exit. Therefore, the rollback stack operation is the same as the object structure of the Back-to-first-out mode. Figure 1 shows the processing process of activity in the current rollback stack at each time point in a time table

Figure 1 shows how to add each new activity in the task to the rollback stack. When you press the button, the current activity is destroyed and the previous activity is restored.

If you continue to press the rollback button, each activity in the stack will pop up in sequence, and the previous activity will be displayed, until the user returns the home screen (or the activity that runs at the beginning of the task ). When all the activities are deleted from the stack, the task no longer exists.

A task is a cohesion unit that can be transferred to the background when a user starts a new task or returns to the home screen through the Home button. In the background, all the activities in the task are terminated, but the rollback stack for this task is completely retained-this task only loses focus when another task occurs, 2,

Figure 2. two tasks: Task B receives user interaction at the foreground, and task a waits for recovery in the background.

A task can be returned to the foreground so that users can retrieve the status when they leave. For example, assume that the current task a has three activities in its stack-two of which are under the current activity. The user presses the Home button and starts a new application from the application initiator. When the new application is displayed on the main screen, task a is transferred to the background. When a new application starts, the system starts Task B with its own activity stack for the application. After interacting with the application of Task B, the user returns to the home page again and selects the application that initially started task. Now, task a returns to the foreground-all three of its stacks are complete and the activity at the top of the stack is restored. At this time point, the user can also return to Task B by clicking the home page button and selecting the application icon for Task B to start (or display the most recent task through the touch screen and control home page button, and select one of them ). This is an example of Android-based multitasking.

Note: You can have multiple tasks in the background at the same time. However, if you run many background tasks at the same time, the system may destroy some background activities to reclaim the memory, resulting in the loss of activity status.

Because the activities in the rollback Stack are not re-arranged, if you are allowed to start a special activity from multiple activities, a new activity instance will be created, and pop up at the top of the stack (instead of bringing the previous activity instance to the top of the stack ). In this way, an activity in your application may be instantiated multiple times (or even from different tasks), as shown in 3.

Figure 3. An activity is instantiated multiple times

In this way, if you use the back-to-back button to navigate back, each instance of the activity is displayed in order of being opened (each with its own UI status ). However, if you do not want an activity to be instantiated multiple times, you can edit the activity. How to edit it will be discussed in the management tasks section later.

Summary of the default behaviors of activity and task:

1. When activity a starts Activity B, activitya is terminated, but the system retains its status (such as the position of the scroll bar and the text of the input form ). If you press the button in Activity B, activity a uses the saved status for restoration.

2. When you exit a task by pressing the Home button, the current activity will be terminated and placed in the background. The system retains the status of each activity in the task. If the user then restores the task by selecting the start icon, the task will come to the foreground and resume the activity at the top of the stack.

3. If you press the rollback button, the current activity will pop up from the stack and be destroyed. The previous activity in the stack is restored. When an activity is destroyed, the system does not keep its status.

4. The activity can be instantiated multiple times, or even from other tasks.

 

 

 

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.