Android task and back stack development documentation translation-2

Source: Internet
Author: User

Managing Tasks
Manage tasks
The way Android manages tasks and the back stack, as described above-by placing all activities started in succession in the same task and in a "last in, first out "stack-works great for most applications and you shouldn't have to worry about how your activities are associated with tasks or how they exist in the back stack.
However, you might decide that you want to interrupt the normal behavior.
Perhaps you want an activity in your application to begin a new task when it is started (instead of being placed within the current task); or, when you start an activity, you want to bring forward an existing instance of it (instead of creating a new instance on top of the back stack); or, you want your back stack to be cleared of all activities before t for the root activity when the user leaves the task.
Android manages tasks and back stacks, as described above-by consecutively placing open activities in the same task and using a stack of "back-in-first-out, for the good work of most applications, you do not have to worry about how to associate your activities with tasks or how they exist in the back stack.
Maybe you want to start a new task when an activity in your application is opened (instead of in the current task); or when you open an activity, you want to bring an existing activity instance to the foreground (instead of creating an instance on the top of the back stack), or when the user leaves the task, except for the root activity, you want to clear all the activities in the stack.

You can do these things and more, with attributes in the <activity> manifest element and with flags in the intent that you pass to startActivity ().
Using the attribute of the list element in <activity> and the identifier passed to the intent of startActivity () function, you can do more

In this regard, the principal <activity> attributes you can use are:
In this regard, the main <activity> attributes you can use include:
TaskAffinity
LaunchMode
AllowTaskReparenting
ClearTaskOnLaunch
AlwaysRetainTaskState
FinishOnTaskLaunch

And the principal intent flags you can use are:
The main intent labels you can use include:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP

In the following sewing, you'll see how you can use these manifest attributes and intent flags to define how activities are associated with tasks and how the behave in the back stack.
In the following sections, you will see how to use these listing attributes and intent flags to define how to associate an activity with a task and how the back stack works.


Defining launch modes
Define Startup Mode
Launch modes allow you to define how a new instance of an activity is associated with the current task. You can define different launch modes in two ways:
The start mode allows you to define how to instantiate an activity associated with the current task.
You can define different startup modes in two ways.

Using the manifest file
1. Use the manifest File
When you declare an activity in your manifest file, you can specify how the activity shoshould associate with tasks when it starts.
When you define an activity in your manifest file, you can specify how the activity is associated with the task when it is opened.

Using Intent flags
2 use the intent flag
When you call startActivity (), you can include a flag in the Intent that declares how (or whether) the new activity shocould associate with the current task.
As such, if Activity A starts Activity B, Activity B can define in its manifest how it shoshould associate with the current task (if at all) and Activity A can also request how Activity B shoshould associate with current task.
If both activities define how Activity B showould associate with a task, then Activity A's request (as defined in the intent) is honored over Activity B's request (as defined in its manifest ).
When you call startActivity (), you can include a flag in intent to declare how (or whether) the new activity should be associated with the current task.
As mentioned above, if Activity A enables Activity B, Activity B can define in its manifest how it should be associated with the current task, and Activity A can also request how Activity B should be associated with the current task.
If both activities define how activity B is associated with A task, the request of Activity A (such as the one defined in intent) the request of Activity B (such as the request defined in manifest) is accepted.

Note: Some launch modes available for the manifest file are not available as flags for an intent and, likewise, some launch modes available as flags for an intent cannot be defined in the manifest.
Note: Some startup modes are available for manifest files, but are unavailable for intent flags. Similarly, some startup modes are available for intent flags but cannot be defined in manifest.


Using the manifest file
Use the manifest File
When declaring an activity in your manifest file, you can specify how the activity shoshould associate with a task using the <activity> element's launchMode attribute.
When you define 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 specifies an instruction on how the activity shoshould be launched into a task.
There are four different launch modes you can assign to the launchMode attribute:
The launchMode attribute specifies how the activity enters an instruction in the task.

"Standard" (the default mode)
Default. The system creates a new instance of the activity in the task from which it was started and routes the intent to it.
The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.
Standard (default mode)
The system creates a new activity in the activity where the task starts and sends intent to the activity according to the route.
An activity can be instantiated multiple times. Each instance can belong to a different task. A task can have multiple identical activity instances.


"SingleTop"
If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent () method, rather than creating a new instance of the activity.
The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity ).
If an activity instance already exists at the top of the current task, the system will send intent to this instance by calling the onNewIntent () method of this instance, instead of creating a new activity instance.
This activity can be instantiated multiple times, and each instance can belong to a different task, in addition, a task can have multiple instances of this activity (but the activity at the top of this back stack is not an existing instance of the activity described above ).


For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top ).
An intent arrives for an activity of type D.
If D has the default "standard" launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D.
However, if D's launch mode is "singleTop", the existing instance of D has es the intent through onNewIntent (), because it's at the top of the stack-the stack remains A-B-C-D.
However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is "singleTop ".
For example, assume that the back stack of A task is composed of four activities: A, B, C, and D, where A is the root activity and the sequence is A, B, C, and D.
The intent of a request type D arrives.
If D uses the standard startup mode, A new D instance is started and the stack changes to A-B-C-D.
However, if D is started in singleTop mode, then the existing D instance receives this intent through onNewIntent () because it is at the top of the stack, the stack maintains the order of A-B-C-D
If an intent of activity B of the Request type is sent, a new instance of B is added to the stack, even if the Startup Mode of B is singleTop.

Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity.
But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived in onNewIntent ().
Note: When an activity is created, you can press the back key to return to the previous activity.
However, when an existing activity processes a new intent, you cannot press the back key to return to the status before the intent reaches the onNewIntent () of the activity.


"SingleTask"
The system creates a new task and instantiates the activity at the root of the new task.
However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent () method, rather than creating a new instance.
Only one instance of the activity can exist at a time.
The system creates a new task and instantiates the activity as the root of the task.
However, an activity instance already exists in a different task. The system will send intent to this instance by calling the onNewIntent () method of the existing instance, instead of creating a new activity instance.
Only one activity instance can exist at the same time

Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.
Although a new task is enabled for this activity, the back key is still returned to the previous activity.


"SingleInstance"
Same as "singleTask", role t that the system doesn't launch any other activities into the task holding the instance.
The activity is always the single and only member of its task; any activities started by this one open in a separate task.
Except that the system does not start any activity to the task that holds the instance, the other activities are the same as those of singleTask.
Activity is always single and the only member of its task. Any dactivity opened by him will be in different tasks.

As another example, the Android Browser application declares that the web browser activity shoshould always open in its own task-by specifying the singleTask launch mode in the <activity> element.
This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application.
Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.
In another example, the Android browser declares that the web browser aicivity should be opened to its own task by specifying the singleTask Startup Mode in the <activity> element.
This means that if your app sends an intent to open the Android browser, the browser activity will not be placed in the same task as your app.

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the Back button always takes the user to the previous activity.
However, if you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground.
At this point, the back stack now has des all activities from the task brought forward, at the top of the stack.
Figure 4 extends strates this type of scenario.

Whether the activity is enabled in a new task or in the same task as the activity that starts it, the back key always brings the user to the previous activity.
If you open an activity with the singleTask startup mode specified, if an instance of this activity exists in the background task, the entire task will be taken to the foreground.
At this time, the back stack contains all the activities of the task to be taken to the foreground and is placed on the top of the stack.
Clarified this situation.

Figure 4.
A representation of how an activity with launch mode "singleTask" is added to the back stack.
If the activity is already a part of a background task with its own back stack, then the entire back stack also comes forward, on top of the current task.
Description of how to add a singleTask activity in startup mode to back stack.
If this activity is already part of a background task, its entire back stack will also come to the foreground, at the top of the current task.

Note: The behaviors that you specify for your activity with the launchMode attribute can be overridden by flags encoded with the intent that start your activity, as discussed in the next section.
Note: You can specify the Startup Mode for your activity by using the identifier in the intent that starts your activity to be overwritten.


Using Intent flags
Use Intent flag

When starting an activity, you can modify the default association of an activity to its task by including flags in the intent that you deliver to startActivity ().
The flags you can use to modify the default behavior are:
When an activity is enabled, you can modify the default association between the activity and its task by including flags in the intent passed to startActivity () function.
You can use flags to modify the default behavior:


FLAG_ACTIVITY_NEW_TASK
Start the activity in a new task.
If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity has es the new intent in onNewIntent ().
Open an activity in a new task
If the activity you are opening is already running in a task, the task will be taken to the foreground and restored to its latest state, and the activity will receive the new intent in onNewIntent ().

This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.
This will lead to the same behavior as the singleTask startup mode, as discussed above.


FLAG_ACTIVITY_SINGLE_TOP
If the activity being started is the current activity (at the top of the back stack), then the existing instance has es a call to onNewIntent (), instead of creating a new instance of the activity.
If this activity is opened on the current activity (on the top of the back stack), The onNewIntent () of the existing instance will be called to create a new instance of this activity.

This produces the same behavior as the "singleTop" launchMode value, discussed in the previous section.
This will lead to the same behavior as singleTop startup mode, as discussed above.


FLAG_ACTIVITY_CLEAR_TOP
If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent ()).
If the activity to be opened is already running in the current task, a new activty instance will no longer be started. Instead, all other activities on the activity will be destroyed, and the intent will be sent to the restored activity through onNewIntent () (now on the top of the task)

There is no value for the launchMode attribute that produces this behavior.
This behavior is caused by no startup mode attribute


FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK.
FLAG_ACTIVITY_CLEAR_TOP is most commonly used in combination with FLAG_ACTIVITY_NEW_TASK.

When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.
When used together, these identifiers are used to locate an activity that already exists in another task and place it in a position that can respond to this intent.


Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent.
That's because a new instance is always created for a new intent when the launch mode is "standard ".
Note: If the activity specifies that the startup mode is standard, it will also be removed from the stack and a new instance will be started to process the arrival intent.
That's because if the startup mode is standard, a new intent will always create a new instance.

 

Author: su1216

Related Article

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.