Android API Guides---Tasks and back Stack

Source: Internet
Author: User
Tags home screen

An application typically contains multiple activities. Each activity should be designed around a specific kind of action that the user can perform and can start other activities. For example, an e-mail application might have an activity to display a list of new messages. When the user selects a message, a new activity opens to view the message.
Activity can even exist on other applications on the device. For example, if your application wants to send an e-mail message, you can define the intent of performing a "send" action, including some data, such as e-mail addresses and messages. The activities of other applications that handle this intent from the declaration itself are then opened. In this case, the purpose is to send an e-mail message, so the e-mail application "Compose" activity starts (if there are multiple activities that support the same intent, then the system lets the user choose which one to use). When sending an email to your activity, and recovering it as if the email activity is part of the application. While activities may come from different applications, Android maintains this seamless user experience by keeping them in the same task activity.
A task is a collection of interactive activities when a user performs certain tasks. The activities are arranged in a stacked (back layer), in which the order in which each activity is opened.
The Device Home screen is the starting position for most tasks. When the user touches the app Launcher icon (or on the home screen?? Shortcut), the application's tasks come to the foreground. If no task exists for the application (the application has not been recently used), then the new task is created with the application's "primary" activity open, such as the root activity in the stack.

When the current activity starts another new activity pushes to the top of the stack and gets the focus. The previous activity remained on the stack, but was stopped. When an activity stops, the system maintains the current state of its user interface. When the user presses the Back button, the current activity pops up from the top of the stack (the activity is destroyed) and the previous activity resumes (its UI's previous state is restored). The activity in the stack never rearranges, only pushes and pops up when the stack pushes, starting with the current activity, when the user leaves it using the back button to pop into the stack. Therefore, the back stack operation is a "last in, first out" object structure. Figure 1 visualizes and illustrates this behavior with a progress schedule that has a stack of current back layers at each point in time.


Figure 1. Each new activity in the task is a representation of how to add a stack behind a project. When the user presses the Back button, the current activity is destroyed and the previous activity is resumed.

If the user continues to press back, each activity on the stack is ejected to show the previous one until the user returns to the home screen (or the task starts when the activity is run). When all the activities are from the stack?? removed, the task no longer exists.


Figure 2. Two tasks: Task B receives the user interaction in the foreground, while task A is in the background, waiting to be restored.


Figure 3: Multiple instantiation of a single activity.
A task is a cohesive unit that can be moved "backstage" when the user starts a new task or goes to the home screen via the home key. In the background, all activities in the task are stopped, but the back of the task's stack remains intact, the task has lost focus at all, and another task occurs, 2. Then a task can be returned to the "foreground" so that the user can pick up them and leave. Assume, for example, that the current task (Task a) has a current activity in its stack of three activities. The user presses the home key and starts a new application from the application. When it appears on the Home screen, task a enters the background. When the new application starts, the system starts the task of the application (Task B) and its own activity stack. After interacting with the application, the user returns home again and chooses to initially start task A. Now, the application, task a comes to the foreground in its stack, all three active activities intact and on top of the stack recovery. At this point, the user can also go home and select the application icon to start the task (or select the application's task from the overview screen) to switch to Task B. This is an example of multitasking on Android.
Note: Multiple tasks can be held simultaneously in the background. However, if the user runs multiple background tasks at the same time, the system may start to destroy the background activity to recover the memory, resulting in the loss of the active state. See the section about activity states below.
Because the activity in the back stack never rearranges, if your application allows the user to start a particular activity from multiple activities, it is to create a new instance of the activity and press it onto the stack (instead of making the top of any instance of the activity). In this way, an activity in the application may be instantiated multiple times (even from a different task), 3 so that if the user navigates backwards using the back button, the order in which each instance of the activity is revealed is opened (each one has its own UI state). However, you can modify this behavior if you do not want an activity to be instantiated more than once. How to do this is discussed in the chapters later in the management task.
Summarize the default behavior for various activities and tasks:
When activity a starts activity B, activity A is stopped, but the system maintains its state (such as scrolling position and entering the form text). If the user presses the return key while in activity B, activity a resumes recovery of its state.
When the user leaves the task by pressing the home button, the current activity stops and its task enters the background. The system retains the status of each activity in the task. If the user later resumes the task by selecting the Launcher icon to start the task, the task involves the foreground and restores the activity on top of the stack.
If the user presses the Back button, the current activity is popped off the stack and destroyed. The previous activity on the stack begins again. When an activity is destroyed, the system does not retain the state of the activity.
An activity can be instantiated multiple times, even from other tasks.
Navigation design
To learn more about how app navigation works, read the navigation guide for Android design on Android.
Energy-saving activity status
As discussed above, when it is stopped the default behavior of the system retains the active state. Thus, when the user navigates back to the previous activity, its user interface appears in the way they left. However, you can and should-proactively retain your active state using callback methods in case the activity is corrupted and must be recreated.
When the system stops one of the activities (when a new activity starts or the task moves to the background, such as), the system may need to restore the system memory to completely destroy the activity. When this happens, information about the state of the activity is lost. If this happens, the system still knows that the activity is stacked one place on the back, but when the activity is taken to the top of the stack, the system must be recreated (rather than recovering it). To avoid losing the user's work, you should take the initiative to persist by implementing the Onsaveinstancestate () callback method of your activity.
For more information about how to save your activity state, see the active document.
management tasks
In the way Android manages for the same tasks, and in the above described will all activities formally start out with "LIFO" Tasks and return stacks, stacks of great works, for most applications, you should not worry about how your activities are related to tasks or how they exist in the back stack. However, you may decide to interrupt normal behavior. Perhaps you want to start a new task when your app's activity starts (rather than in the current task), or, when you start an activity, you want to present an existing instance of it (instead of creating a new instance at the top of the stack at the back); or, you want all the activities of your back stack, The root activity is cleared except when the user leaves the task.
You can do many of these things with the < activity > manifest element, and with the intent flag attribute, you pass it to StartActivity ().
In this regard, the main < activities > properties that you can use are as follows:
Taskaffinity
Launchmode
Allowtaskreparenting
Cleartaskonlaunch
Alwaysretaintaskstate
Finishontasklaunch
The main intent flags you can also use are:
Flag_activity_new_task
Flag_activity_clear_top
Flag_activity_single_top
In the following sections, you will see how these manifest attributes and intent flags are used to define how the activity relates to the task, and how they behave on the back stack.
In addition, a separate discussion is how the task and activity fees can be expressed and managed in the overview screen. See the overview screen for more information. Typically, you should let the system define how your tasks and activities are represented in the overview screen, and you do not need to modify this behavior.
Note: Most applications should not break the default behavior of activities and tasks. If it is determined that it is necessary to modify the default behavior for your activity, use caution and ensure that the test activity is available during launch, as well as the back button navigation of other activities and tasks back to it. Be sure to test the behavior navigation that might conflict with the user's expected behavior.
Defining the startup mode
The startup mode allows you to define a new instance of an activity associated with the current task. You can define different startup modes in two ways:
Working with manifest files
When you declare an activity in a manifest file, you can specify that the activity should be associated with the task when it starts.
Sign of intention to use
When you call StartActivity (), you can include a flag in intent that declares how the new activity should or is associated with the current task.
Therefore, if activity a starts activity B, activity B can also ask how activity B should be associated with the current task in its manifest definition of how it should be associated with the current task (if any) and activity a. If these two activities define how activity B should be associated with a task, then activity A's request (such as intent definition) is honored at Activity B's request (defined in its manifest).
Note: Some startup modes that can be used for manifest files are not intended as flags, and similarly, some startup modes that are intended as flags cannot be defined in the manifest.
Working with manifest files
When declaring an activity in a manifest file, you can specify how the activity should use the < activity > element's Launchmode Property for the task Association.
The Launchmode property specifies how the activity should initiate instructions to the task. There are four startup modes that can be assigned to different Launchmode properties:
"Standard"(default mode)
Default. The system creates a new instance of the activity from which it is launched and the route intends it to work on the task. The activity can be instantiated multiple times, each instance can belong to a different task, and a task can have multiple instances.
"Singletop"
If the activity of a real?? The example already exists at the top of the current task, and the system routes the instance through its Onnewintent () method call instead of creating the intent of the new instance of the activity. The activity can be instantiated several times, each instance can belong to a different task, and a task can have multiple instances (but only if the activity on the top of the back stack is inactive on an existing instance).
For example, assume that the back stack of a task is composed of the active b,c and D on the root of the above (stack a-b-c-d,d is top). Intention to reach the activity of type D. If D has a default "standard" rollout mode, the class launches a new instance, and the stack becomes a-b-c-d-d. However, if D's hair?? The shot mode is "Singletop", and the existing instance of D receives the intent through onnewintent () because it is the a-b-c-d that remains on top of the stacked stack. However, if the intention is to reach the activity for Type B, a new instance of B is added to the stack, even if its transmit mode is "Singletop".
Note: When creating an active new scenario, the user can press the Back button to return to the previous activity. However, when an existing instance of an activity processes the new intent, the user cannot press the Back button to return to the active state of the new intent, before the Onnewintent arrives ().
"Singletask"
The system creates a new task and the root instantiation activity of the new task. However, if a separate task already exists in the active instance, the system is routed through its onnewintent () method call instead of creating an existing instance of the new instance intent. Only one instance of the activity can exist at a time.
Note: Although the activity starts at a new task, the Back button still brings the user back to the previous activity.
"SingleInstance".
The same as "Singletask", the difference is that the system does not launch any other activities into the task of maintaining the instance. Activity is always its task order, the only member of any activity, through which one opens in a separate task.
Another example is that the Android browser application declares the activity of the Web browser to be open frequently in its own task, by specifying the < activity > element Singletask startup mode. This means that if your application sends out the intention to open the Android browser, it's activity is not placed on the same task as your application. Instead, whether it's a new task-when it's launched for the browser, or if the browser is already running a task in the background, the task is raised to handle the new intent.

Regardless of whether the activity starts on a new task or on the same task, starting its activity, the back button always puts the user past the activity. However, if you start to specify the activity of the Singletask startup mode, then the activity is a real?? example, a background task exists, and the entire task is brought to the foreground. At this point, the back stack now includes all activities from the task presented, at the top of the stack. Figure 4 shows this type of scenario.


Figure 4 shows how the activity of the transmit mode "Singletask" is added to the return stack. If the activity is part of a background task that has its own back layer, the entire back overlay also comes forward, at the top of the current task.
For more information about using the launch mode in the manifest file, see the < activities > elements document, where Launchmode properties and accepted values are discussed more.
Note: The activity behavior of the Launchmode property that you specify can be overridden by including an intent flag with the start activity, as discussed in the next section.
Sign of intention to use
When starting an activity, you can change the default association of the activity to its task by marking the intent that you pass to StartActivity (). The flags you can use to modify the default behavior are:
Flag_activity_new_task
The activity that starts a new task. If the task is already running for you now also, make sure that the task is brought to the foreground, its final state is restored, and the onnewintent New Intent () is received in the activity.
This produces the same behavior as the "Singletask" Launchmode value, as discussed in the previous section.
Flag_activity_single_top
If the activated activity is the current activity (at the top of the stack at the back), then an existing instance is received instead of creating a new instance of the activity) called Onnewintent (,.
This produces the same behavior as the "Singletop" Launchmode value, as discussed in the previous section.
Flag_activity_clear_top
If the activity that is being started is already running in the current task, then a new instance of the activity is destroyed in other activities above it, and this intent is passed to the instance of the recovery activity (this is the top), through Onnewintent ()).
There is no value for the Launchmode attribute that produces this behavior.
Flag_activity_clear_top is most commonly used with flag_activity_new_task. When used together, these flags are a way of locating an existing activity on another task and putting it in a position where it can give an intent response.
Note: If the specified active startup mode is "standard", it also removes a new instance from the stack in its bit to handle the incoming intent to start. This is because a new instance is always created for when the new intent to launch the pattern is "standard".
Handling affinity
Affinity indicates which task the activity prefers to belong to. By default, all are derived from the same application's activity to each other's affinity. This way, by default, all activities in the same application tend to be in the same task. However, you can modify the default affinity for an activity. In the same application definition different apps define the affinity that can be shared with affinity activities, or activities can be assigned different tasks.
You can modify affinity with < activity > element properties taskaffinity any given activity.
The Taskaffinity property requires a string value that must declare the default package name from the < manifest > element, because the system will use that name to identify the application's default task affinity.
Affinity entry in two cases:
When starting an activity intent contains the FLAG_ACTIVITY_NEW_TASK flag.
A new activity, in which the injection invokes the StartActivity () active task by default. It pushes to the same back-stack caller. However, if the intent passed to StartActivity () contains the FLAG_ACTIVITY_NEW_TASK flag, the system will look for different tasks to accommodate the new activity. Typically, this is a new subject. However, this does not have to be the case. If there is already an existing task with the same affinity as the new activity, the activity is emitted to that task. If not, it starts a new task.
If this flag causes the activity to start a new task and the user presses the home key to leave it, there must be some way for the user to navigate back to the task. Some entities (such as Notification Manager) always start an external task with an activity that has never been as part of themselves, so they always put flag_activity_new_task on their intentions by giving StartActivity (). If you have an activity that can be invoked by an external entity that may use this flag, take the user to a separate way to get back to that age, such as using the Launcher icon (the task of the root activity of the task Nursing has the Category_launcher intent filter; see the Startup Tasks section below).
When an activity has its Allowtaskreparenting property set to True.
In this case, the activity can move from the task it has affinity to, when the task involves the foreground.
For example, suppose that the reported weather conditions in the selected city's activity are defined as part of a tourism application. It has the same affinity as other activities in the same application (the default application affinity), which allows the re-parenting to have this attribute. When one of your activities starts, the weather reports the activity of the gene, which originally belonged to the same task in the event. However, when the mission of the travel application involves the foreground, the weather report activity is reassigned to the task and displayed inside.
Tip: If an. apk file contains but more than one "application" from the user's perspective, you may want to use the Taskaffinity property to assign different affinity to each "application" related activity.
Clear Stack back
If the user leaves the task for a long time, the system clears all active tasks except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, the user may have abandoned the task they were doing before and are returning to start something new.
There are some activity properties that you can use to modify this behavior:
Alwaysretaintaskstate
If this attribute is in any?? The root activity of the service is set to "true" and the default behavior just described does not occur. The task is persisted in all activities in the stack, even after a long time.
Cleartaskonlaunch
If this attribute is in any?? The root activity of the service is set to "true" and the stack is cleared down whenever the user leaves the task and returns to its root vigor. In other words, it is alwaysretaintaskstate opposite. The user always returns to its initial state of the task when the departed task is only one moment, even after that.
Finishontasklaunch
The property is like Cleartaskonlaunch, but it runs in a single activity instead of a complete task. It may also cause any activity to go away, including root activity. When it is set to "true", the activity is still only part of the task for the current session. If the user leaves and then returns to the task, this is no longer present.
Start a task
You can set an activity as the Pointcut task by giving it an intent filter with "Android.intent.action.MAIN" for the specified action and "Android.intent.category.LAUNCHER" for 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>

Such an intent filter would cause the activity to be displayed in the application startup, give the user the means to carry out the activity, and return to the icon and label of the task it created after it was started any time.
This second ability is important: the user must be able to leave a task and then come back later to use the active launcher. For this reason, the standard activity always starts a task, "Singletask" and "SingleInstance" two launch modes, which should be used only if the activity has Action_main and Category_launcher filters. Imagine, for example, what happens if a filter is lost: an intention to start a "singletask" activity, start a new task, and the user takes some time to complete the task. The user then presses the home key. Tasks are now sent to the background and are not visible. Now, the user has no way to return the task because it is not represented in the application startup.
For those cases, you don't want users to be able to go back to an activity, set < activity > element finishontasklaunch to "true" (see??). Clear stack).
Further information on how to represent and manage the overview screen for tasks and activities the overview screen is available.

Android API Guides---Tasks and back Stack

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.