Android Api Component-Translation Task and rollback Stack (Tasks and Back Stack), androidstack
An application usually contains multiple activities. Each activity should be designed around the types of actions that a specified user can execute and can enable other activities. For example, an emali application may have an activity that displays a list of new messages. When you select a message, a new activity opens to view the message.
An activity can even enable the activity of other applications on the device. For example, if your application sends an email message, you can define an intent to execute a "send" action and contain data such as the email address and message. The activity from another application defines that it can be opened by itself using the intent. In this example, the intent will send an email, so the "compose" activity of an email application is enabled (if multiple activities support the same intent, then the system will let the user select which one to use ). After an email is sent, your activity is restored and it is like this email activity is part of your application. Although the activities between applications are different, Android maintains the user experience by keeping all the activities in the same task seamlessly.
A task is a set of activities that interact with users when a job is executed. Activity is arranged in a stack (rollback stack), and each activity is opened in the order of entry.
The Home screen of the device is where most tasks start. When a user presses an icon in an application initiator, the application task is transferred to the foreground. If no task exists for this application (the application has been used recently ), A new task is created and the "main" activity opens the application with the root activity in that stack.
When the current activity opens another activity, the new activity is pushed to the top of the stack and the focus is obtained. The previous activity was either in the war, but it was stopped. When an activity stops, the system recycles the status of the current user interface. When you press the return button, the current activity is popped up from the top of the stack (this activity is destroyed) and the previous activity is restored (the previous UI status is restored ). Activity users in the stack will not be reorganized, only push from Stack and pop-up-push to stack when enabled in current activity and pop-up when the user uses the return button to exit. In this way, the rollback stack operation is used as an object structure of "first-in-first-out. The timeline shows how to use the current rollback stack to process every point on time between activities.
This figure shows how each new activity adds items to the background stack in the task. When you press the rollback button, the current activity is destroyed and the previous activity starts again.
If the user continues to press return, each activity in the stack will be displayed to display the previous activity, it is unknown until the user returns to the home screen (or when the task starts, no matter which activity is running ). When all the activities are removed from the stack, the task no longer exists.
When a user starts a new task or enters the Home screen through the Home key, a task is a continuous unit that can be moved to the "background. Then in the background, all the activities in the task will be stopped, but the rollback stack for this task is still complete-indicates that when another task is in progress, this task only loses focus, as shown in. A task can then be returned to the "front-end" so that the user can get the location where they left. Join. For example, the current Task (Task A) has three activities in its stack-two under the current activity. The user presses the Home Key and starts a new application from the application initiator. When the main screen appears, Task A enters the background. When a new application starts, the system starts a Task for this application (Task B) with the stack of its own activity. After interacting with the application, the user returns Home and selects the original started Task. Now, Task A comes to the front-end-all three of the activities in its stack are complete and the activity at the top of the stack starts again. In this case, you can switch to Home to return to Task B and select the application icon of the started Task (or select the Task of the app from the overview screen ). This is an example of multitasking on Android.
Note: multi-task operations can be performed immediately in the background. However, if a user runs many background tasks at the same time, the system may destroy Background Activities in the order of memory recycle, resulting in the loss of the status of those activities. For Activity state, see the following section.
Because the activity in the rollback stack will never be reorganized, if your application allows the user to start more than one special activity, the stack where the instance of the activity is created and pushed (instead of bringing it to any activity instance ). In this way, an activity in your application may be initialized multiple times (or even from different tasks), just as it is displayed. In such a case, if the user uses the return button to navigate and return, each activity instance is displayed in the order they are opened (each with their own UI status ). However, if you do not want an activity to be initialized multiple times, you can modify the activity. How to do this will be discussed in the following Managing Tasks section.
To summarize the activity and task behaviors:
When Activity A enables Activity B, Activity A is stopped, but the system keeps its status (such as scrolling position and text in the input form ). If you press the return button in Activity B, Activity A uses its status to start being restored.
When you exit the task by pressing the Home key, the current activity is stopped and its task enters the background. The system retains the status of each activity in the task. If you select the initiator icon to start the task again, the task will enter the front-end and re-start the activity at the top of the stack.
If you press the return button, the current activity is popped up from the stack and destroyed. The activity in front of the stack is restarted. When an activity is destroyed, the system does not keep its status.
The activity can be initialized multiple times or even from other tasks.
Navigation Design
For apps working in android, read the Android Design's Navigation document.
Manage tasks
The Android management task and stack rollback method described above, by placing all started activities in the same task and using the "back-in-first-out" Stack-It plays a lot of role for most applications and you don't need to worry about how your activity task association or how they exist in the rollback stack. However, you may decide what you want the terminal to do normally. Maybe you want to start a new task when the activity in your application is started (instead of in the current task); or when you start an activity, you want to bring an existing activity instance to the Front (instead of creating a new instance at the top of the stack ); or when the user leaves the task, you want your rollback stack to clear all the activities except the root activity.
You can use this attribute in the <activity> list element to do those things or more things, and take the flag that you want to pass to the intent in startActivity.
In this regard, the main <activity> attributes you can use are:
TaskAffinity
LaunchMode
AllowTaskReparenting
ClearTaskOnLaunch
AlwaysRetainTaskState
FinishOnTaskLaunch
You can also use the main intent flag:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
In the following section, you will see how to use the listing attributes and intent flag to define how the activity is associated with the task and how they run in the rollback stack.
In addition, the separate discussion of activity and task takes into account that the task and activity may be described or managed in that summary screen. View More Overview Screen information. Generally, you should allow the system to define that your task and activity are described on the comprehensive screen, and you do not need to modify this behavior.
Note: Most applications do not send default behaviors to activities and tasks. If you decide that your activity needs to be the default behavior, use a warning and make sure that the activity availability is rolled back during runtime testing and from other activities and tasks by using the return button navigation to it. Ensure that the test navigation behavior conflicts with the user's expected behavior.
Define Startup Mode
The startup mode allows you to define a new activity instance if it is associated with the current task. You can define different startup modes in two ways:
Usage list
When you define an activity in your manifest file, you can specify how the activity is associated with the task at the beginning.
Use the flag of Intent
When you call startActivity (), you should include a flag in the Intent, which defines how (or whether) the new activity should be associated with the current task.
For example, if Activity A enables Activity B, Activity B can define how it is associated with the current task (or all) in its manifest, in addition, Activity A can also ask how Activity B should be associated with the current task. If both activities define how activity B is associated with A task, the requirement of Activity A (defined in intent) comply with the requirements of Activity B (defined in its manifest ).
Note: Some valid start modes are invalid for the intent flag of the manifest file. Similarly, some valid start modes for the intent flag cannot be defined in manifest.
Use the manifest File
When defining an activity in your manifest file, you can specify how the activity is associated with a task using the lanuchMode attribute of the <activity> element.
This lanuchMode attribute specifies a command about how the activity should be run into a task. There are four different startup modes that you can assign to the launchMode attribute:
"Standard" (default mode)
By default, the system creates an activity instance in the task, which is enabled from the task and routes intent to the activity. This activity can be initialized multiple times. Each instance may belong to a different task, and a task may have multiple instances.
"SingleTop"
If an activity instance already exists at the top of the current task, the system routes the intent to that instance by calling its onNewIntent (), but it does not create a new activity instance. This activity may be initialized multiple times, each instance may belong to a different task, and a task may have multiple instances (but if this activity is at the top of the rollback stack, only one activity instance exists ).
For example, activity B, C, and D are added to the background stack of A task to form the Root activity A at the top. An intent for activity D arrives. If D has the default "standard" startup mode, A new class instance will be started and the stack will become A-B-C-D. However, if the Startup Mode of D is "singleTop", then the existing instance of D receives the intent through onNewIntent, because he is at the top of the stack-this stack is either A-B-C-D. However, if an intent arrives for activity B, an instance of activity B will be added to this stack, even if its startup mode is "singleTop ".
Note: When an activity instance is created, the user may return to the previous activity by clicking the "back" button. However, when an activity instance already exists, a new intent is used. Before the new intent arrives in onNewIntent (), the user cannot return to the status of the previous activity by pressing the return button.
"SingleTask"
The system creates a new task and initializes the activity at the root of the new task. However, if the instance of the activity already exists in a separate task, the system routes the intent to the instance of the existing activity by calling its onNewIntent () method, instead of creating a new instance. At the same time, only one activity instance can exist.
Note: although the activity starts in a new task, the rollback button can still return to the previous activity.
"SingleInstance"
Except that the system does not start any other activity to enter the task that holds this instance, the other is the same as "singleTask. This activity is always single, and its task has only one member. Any opened activity opens through this split task.
As another example, the Android browser application defines that the web browser should always open in its own task by specifying the singleTask Startup Mode in the <activity> element. This means that if your application publishes an intent that opens the Android Browser, its activity will not be placed in the same task as your application. Instead, start a new task to open the browser. If the browser is already running in the background task, the task will be taken to the front to operate the new intent.
Whether or not an activity is enabled in a new task or the same task, it is like this activity is enabled, and the rollback button always takes the user to the previous activity. However, if you start an activity with the singleTask startup mode specified, if an activity instance exists in a background task, the whole task will be taken to the front. At this time, the background stack now contains all the activities from the task that is taken to the front at the top of the stack. For example:
For more information about how to use the Startup Mode in the manifest file, see the <activity> element document, where the lanuchMode attribute and received value are discussed more.
Note: The behaviors you specify for your activity with the launchMode attribute can be overwritten by the flags in Intent to enable your activity. This will be discussed later.
Use the flag of Intent
When you start an activity, you may modify the default associated task of an activity by using the flag contained in the intent you passed to startActivity. You can use these flags to modify the default line as follows:
FLAG_ACTIVITY_NEW_TASK
Enable activity in a new task. If a task is already running for the activity you are currently enabling, the task will be taken to the front end with its final state restored and the activity is in onNewIntent () to receive a new intent.
This generated behavior is similar to the "singleTask" launchMode value discussed earlier.
FLAG_ACTIVITY_SINGLE_TOP
If the active activity is the current activity (on the top of the rollback stack), this existing instance will receive a call to onNewIntent () instead of creating a new activity instance ().
This produces the same behavior as the "singleTop" launchMode value discussed earlier.
FLAG_ACTIVITY_CLEAR_TOP
If the started activity is already running in the current task, all other activities on the activity will be destroyed instead of a new instance running the activity and the onNewIntent () this intent is passed to the instance of the started activity.
This behavior is generated without the launchMode attribute value.
FLAG_ACTIVITY_CLEAR_TOP is the longest combination with FLAG_ACTIVITY_NEW_TASK. When we use it together, the flag is a method to localize an activity that already exists in other ideas and place it in a place that can respond to intent.
Note: If the specified Startup Mode of this activity is "standard ", it will also be removed from the stack and a new instance will be started at its location to operate the intent. This is because when the start mode is "standard", a new instance is always created for a new intent.
Handling affinities)
This relationship indicates which task an activity prefers. By default, all activities from the same application have a relationship with each other. Therefore, by default, all activities in the same application prefer to be in the same task. However, you can modify the default link for the activity. Activity defined in different applications can share a relationship, or activity defined in the same application can be arranged in different task relationships.
You can use the taskAffinity attribute in the <activity> element to modify the relationship between the given activity.
The taskAffinity attribute takes a string value. The attribute defined in the <manifest> element must be unique in the default package name, because the system gives the application a name to distinguish the default task relationship.
In the two environments, this relationship starts to take effect:
When startActivity () is called to start an activity, the intent that contains the FLAG_ACTIVITY_NEW_TASK mark. Your new activity is run in the activity task by default. It is pushed to the same background stack as the caller. However, if the intent passed to startActivity () contains the FLAG_ACTIVITY_NEW_TASK tag, the system will find a different task for the new activity space. Generally, it is a new task. However, it does not need to be. If a new activity has the same relationship with an existing task, the activity is started into the task. If not, it starts a new task.
If this tag causes an activity to start a new task and the user presses the Home Key to leave it, there must be some ways to navigate back to the task. Some entities (such as notification Management) always enable the activity in an internal task and will never be part of it. Therefore, they always put FLAG_ACTIVITY_NEW_TASK into the intent they pass to startActivity. If you have an activity that can be called through internal entities, this tag may be used. Note that you have an independent way to return to the task they opened, like using the launcher icon (the root activity of the task has a CATEGOR_LAUNCHER intent filter; see the Starging a task section below ).
When an activity sets its allowTaskReparenting attribute to "true ".
In this example, when a task comes to the front end, the activity can be removed from the task with a link enabled.
For example, assume that the activity status of a weather forecast in the selected city is defined as the part of the travel application. It has the same relationship with other activities in the same application (Default Application relationship) and it allows the parent class to use this attribute. When one of your activities enables the weather forecast activity, it initially belongs to the same task as your activity. However, when the travel application task returns to the foreground, the weather forecast activity is arranged in that task and displayed in it.
Suggestion: If a .apk file contains more than one "application" from your perspective, you may want to use the taskAffinity attribute to allocate different relationships to the activities associated with each "application.
Clear rollback Stack
If the user leaves a task for a long time, the system will clear all the activities except the root activity in the task. When the user returns a task, only the root activity is restored. The way the system performs, because after a long time, users may have abandoned what they were doing and returned to the task to start new things.
You can use some attributes of the activity to modify this behavior:
AlwaysRetainTaskState
If this attribute is set to "true" in the Root activity of the task, the default behavior described will not happen. This task recycles all the activities in its task, even after a period of time.
ClearTaskOnLaunch
If this attribute is set to "true" in the Root activity of a task, the stack will be cleared to the root activity no matter when the user leaves and returns. In other words, it is the opposite of alwaysRetainTaskState. The user always returns to the initial status of the task, even after a long time of leaving the task.
FinishOnTaskLaunch
This attribute is like clearTaskOnLaunch, But it operates on a single activity rather than the entire task. It can also cause any activity to exit, including the root activity. When it is set to "true", this activity is still part of this task in the current session. If the user leaves and returns to this task, it is no longer displayed.
Start a task
You can set an activity as the entry point of a task. intent. action. the MAIN "filter is used as the specified action and" android. intent. category. LAUNCHER "is used as the specified category. For example:
<activity ...> <intent-filter ...> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter></activity>
An intent filter aims to display an icon and tag for the activity in the application launcher, you can create a task at any time after the activity is started.
This second feature is important: the user must be able to leave the task and then use the activity starter to return. For this reason, the two startup modes mark activity as if they always initialize a task, "singleTask" and "singleInstance ", it should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. Imagine what might happen if a filter is lost: an intent starts a "singleTask" activity, initializes a new task, and the user spends some time in that task. The user then presses the Home Key. The task is now sent to the background and invisible. The user does not return to this task because it is not described in the application initiator.
If you do not want the user to return to an activity, set finishOnTaskLaunch of the <activity> element to "true" (see Clearing the stack ).