google 官网上关于launchMode的解释,以及使用四种不同mode时对task和back stack的影响,以后有时间再翻译总结一下。
Tasks and Back Stack
An application usually contains multiple activities. Each activity should bedesigned around a specific kind of action the user can perform and can startother activities. For example, an email application might have one activity toshow a list of new messages. When the user selects a message, a new activityopens to view that message.
An activity can even start activities thatexist in other applications on the device. For example, if your applicationwants to send an email message, you can define an intent to perform a"send" action and include some data, such as an email address and amessage. An activity from another application that declares itself to handlethis kind of intent then opens. In this case, the intent is to send an email,so an email application's "compose" activity starts (if multipleactivities support the same intent, then the system lets the user select whichone to use). When the email is sent, your activity resumes and it seems as ifthe email activity was part of your application. Even though the activities maybe from different applications, Android maintains this seamless user experienceby keeping both activities in the same task.
A task is a collection of activities thatusers interact with when performing a certain job. The activities are arrangedin a stack (theback stack), inthe order in which each activity is opened.
The device Home screen is the starting placefor most tasks. When the user touches an icon in the application launcher (or ashortcut on the Home screen), that application's task comes to the foreground.If no task exists for the application (the application has not been usedrecently), then a new task is created and the "main" activity forthat application opens as the root activity in the stack.
When the current activity starts another,the new activity is pushed on the top of the stack and takes focus. Theprevious activity remains in the stack, but is stopped. When an activity stops,the system retains the current state of its user interface. When the userpresses the Backbutton, the current activity is popped from the top of the stack (the activityis destroyed) and the previous activity resumes (the previous state of its UIis restored).
Activities in the stack are neverrearranged, only pushed and popped from the stack—pushed onto the stack whenstarted by the current activity and popped off when the user leaves it usingtheBackbutton. As such, the back stack operates as a "last in, first out"object structure. Figure 1 visualizes this behavior with a timeline showing theprogress between activities along with the current back stack at each point intime.
Figure1. A representation of how each new activity in a task addsan item to the back stack. When the user presses theBack button, the currentactivity is destroyed and the previous activity resumes.
If the user continues to press Back, then each activity inthe stack is popped off to reveal the previous one, until the user returns tothe Home screen (or to whichever activity was running when the task began).When all activities are removed from the stack, the task no longer exists.
Figure2. Two tasks: Task B receives user interaction in theforeground, while Task A is in the background, waiting to be resumed.
Figure3. A single activity is instantiated multiple times.
A task is a cohesive unit that can move tothe "background" when users begin a new task or go to the Homescreen, via theHomebutton. While in the background, all the activities in the task are stopped,but the back stack for the task remains intact—the task has simply lost focuswhile another task takes place, as shown in figure 2. A task can then return tothe "foreground" so users can pick up where they left off. Suppose,for example, that the current task (Task A) has three activities in itsstack—two under the current activity. The user presses theHome button,then starts a newapplication from the application launcher. When the Home screen appears, Task Agoes into the background. When the new application starts, the system starts atask for that application (Task B) with its own stack of activities. Afterinteracting with that application, the user returns Home again and selects theapplication that originally started Task A. Now, Task A comes to theforeground—all three activities in its stack are intact and the activity at thetop of the stack resumes. At this point, the user can also switch back to TaskB by going Home and selecting the application icon that started that task (orby selecting the app's task from the overview screen). This is an example ofmultitasking on Android.
Note:Multiple tasks can be held in the background at once. However, if the user isrunning many background tasks at the same time, the system might begindestroying background activities in order to recover memory, causing theactivity states to be lost. See the following section aboutActivity state.
Because the activities in the back stack arenever rearranged, if your application allows users to start a particular activityfrom more than one activity, a new instance of that activity is created andpushed onto the stack (rather than bringing any previous instance of theactivity to the top). As such, one activity in your application might beinstantiated multiple times (even from different tasks), as shown in figure 3.As such, if the user navigates backward using theBack button, each instance ofthe activity is revealed in the order they were opened (each with their own UIstate). However, you can modify this behavior if you do not want an activity tobe instantiated more than once. How to do so is discussed in the later sectionaboutManaging Tasks.
To summarize the default behavior foractivities and tasks:
· When Activity A starts Activity B, ActivityA is stopped, but the system retains its state (such as scroll position andtext entered into forms). If the user presses theBack button while in ActivityB, Activity A resumes with its state restored.
· When the user leaves a task by pressing theHome button, the currentactivity is stopped and its task goes into the background. The system retainsthe state of every activity in the task. If the user later resumes the task byselecting the launcher icon that began the task, the task comes to theforeground and resumes the activity at the top of the stack.
· If the user presses theBack button, the currentactivity is popped from the stack and destroyed. The previous activity in thestack is resumed. When an activity is destroyed, the systemdoes not retain the activity'sstate.
· Activities can be instantiated multipletimes, even from other tasks.
NavigationDesign
For more about how app navigation works onAndroid, read Android Design'sNavigation guide.
Saving Activity State
Asdiscussed above, the system's default behavior preserves the state of anactivity when it is stopped. This way, when users navigate back to a previousactivity, its user interface appears the way they left it. However, you can—andshould—proactively retainthe state of your activities using callback methods, in case the activity isdestroyed and must be recreated.
Whenthe system stops one of your activities (such as when a new activity starts orthe task moves to the background), the system might destroy that activitycompletely if it needs to recover system memory. When this happens, informationabout the activity state is lost. If this happens, the system still knows thatthe activity has a place in the back stack, but when the activity is brought tothe top of the stack the system must recreate it (rather than resume it). Inorder to avoid losing the user's work, you should proactively retain it byimplementing the onSaveInstanceState()callback methods in your activity.
Formore information about how to save your activity state, see theActivitiesdocument.
Managing Tasks
Theway Android manages tasks and the back stack, as described above—by placing allactivities started in succession in the same task and in a "last in, firstout" stack—works great for most applications and you shouldn't have toworry about how your activities are associated with tasks or how they exist inthe back stack. However, you might decide that you want to interrupt the normalbehavior. Perhaps you want an activity in your application to begin a new taskwhen it is started (instead of being placed within the current task); or, whenyou 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 wantyour back stack to be cleared of all activities except for the root activitywhen the user leaves the task.
Youcan do these things and more, with attributes in the<activity>
manifest elementand with flags in the intent that you pass tostartActivity().
Inthis regard, the principal <activity>
attributes youcan use are:
Andthe principal intent flags you can use are:
Inthe following sections, you'll see how you can use these manifest attributesand intent flags to define how activities are associated with tasks and howthey behave in the back stack.
Also,discussed separately are the considerations for how tasks and activites may berepresented and managed in the overview screen. SeeOverview Screenfor more information. Normally you should allow the system to define how yourtask and activities are represented in the overview screen, and you don't needto modify this behavior.
Caution:Most applications should not interrupt the default behavior for activities andtasks. If you determine that it's necessary for your activity to modify thedefault behaviors, use caution and be sure to test the usability of theactivity during launch and when navigating back to it from other activities andtasks with theBack button. Besure to test for navigation behaviors that might conflict with the user'sexpected behavior.
Defining launch modes
Launchmodes allow you to define how a new instance of an activity is associated withthe current task. You can define different launch modes in two ways:
When you declare an activity in your manifest file, youcan specify how the activity should associate with tasks when it starts.
When you call startActivity(), you can include a flag in the Intent that declares how (or whether) the newactivity should associate with the current task.
Assuch, if Activity A starts Activity B, Activity B can define in its manifesthow it should associate with the current task (if at all) and Activity A canalso request how Activity B should associate with current task. If bothactivities define how Activity B should associate with a task, then ActivityA's request (as defined in the intent) is honored over Activity B's request (asdefined in its manifest).
Note: Some launch modesavailable 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 definedin the manifest.
Using the manifestfile
Whendeclaring an activity in your manifest file, you can specify how the activityshould associate with a task using the<activity>
element'slaunchMode
attribute.
ThelaunchMode
attribute specifies aninstruction on how the activity should be launched into a task. There are fourdifferent launch modes you can assign to thelaunchModeattribute:
"standard"
(the default mode)
Default. The system creates a new instance of the activityin the task from which it was started and routes the intent to it. The activitycan be instantiated multiple times, each instance can belong to differenttasks, and one task can have multiple instances.
"singleTop"
If an instance of the activityalready exists at the top of the current task, the system routes the intent tothat instance through a call to itsonNewIntent() method, rather than creating a newinstance of the activity. The activity can be instantiated multiple times, eachinstance can belong to different tasks, and one task can have multipleinstances (but only if the activity at the top of the back stack isnot an existing instance of the activity).
For example, suppose a task's back stack consists of rootactivity A with activities B, C, and D on top (the stack is A-B-C-D; D is ontop). An intent arrives for an activity of type D. If D has the default"standard"
launch mode, anew 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 receives the intent throughonNewIntent(), because it's at the top of thestack—the stack remains A-B-C-D. However, if an intent arrives for an activityof type B, then a new instance of B is added to the stack, even if its launchmode is"singleTop"
.
Note: When a new instance of an activity iscreated, the user can press theBack button toreturn to the previous activity. But when an existing instance of an activityhandles a new intent, the user cannot press theBackbutton to return to the state of the activity before the new intent arrived inonNewIntent().
"singleTask"
The system creates a new task andinstantiates the activity at the root of the new task. However, if an instanceof the activity already exists in a separate task, the system routes the intentto the existing instance through a call to its onNewIntent() method, rather than creating a newinstance. Only one instance of the activity can exist at a time.
Note: Although the activity starts in a new task,theBack button still returns the userto the previous activity.
"singleInstance"
.
Same as "singleTask"
,except that the system doesn't launch any other activities into the taskholding the instance. The activity is always the single and only member of itstask; any activities started by this one open in a separate task.
Asanother example, the Android Browser application declares that the web browseractivity should always open in its own task—by specifying thesingleTask
launch mode in the <activity>
element. Thismeans that if your application issues an intent to open the Android Browser,its activity is not placed inthe same task as your application. Instead, either a new task starts for theBrowser or, if the Browser already has a task running in the background, thattask is brought forward to handle the new intent.
Regardlessof whether an activity starts in a new task or in the same task as the activitythat started it, theBackbutton always takes the user to the previous activity. However, if you start anactivity that specifies thesingleTask
launch mode, then if an instance of thatactivity exists in a background task, that whole task is brought to theforeground. At this point, the back stack now includes all activities from thetask brought forward, at the top of the stack. Figure 4 illustrates this typeof scenario.
Figure 4.A representation of how an activity with launch mode "singleTask" isadded to the back stack. If the activity is already a part of a background taskwith its own back stack, then the entire back stack also comes forward, on topof the current task.
Formore information about using launch modes in the manifest file, see the<activity>element documentation, where thelaunchMode
attribute and the accepted values are discussedmore.
Note: The behaviorsthat you specify for your activity with thelaunchMode
attribute can beoverridden by flags included with the intent that start your activity, asdiscussed in the next section.
Using Intent flags
Whenstarting an activity, you can modify the default association of an activity toits task by including flags in the intent that you deliver tostartActivity().The flags you can use to modify the default behavior are:
Start the activity in a new task. Ifa task is already running for the activity you are now starting, that task isbrought to the foreground with its last state restored and the activityreceives the new intent inonNewIntent().
This produces the same behavior as the "singleTask"
launchMode
value, discussed in theprevious section.
If the activity being started is thecurrent activity (at the top of the back stack), then the existing instancereceives a call toonNewIntent(), instead of creating a new instanceof the activity.
This produces the same behavior as the "singleTop"
launchMode
value, discussed in theprevious section.
If the activity being started isalready running in the current task, then instead of launching a new instanceof that activity, all of the other activities on top of it are destroyed andthis intent is delivered to the resumed instance of the activity (now on top),through onNewIntent()).
There is no value for the launchMode
attribute that producesthis behavior.
FLAG_ACTIVITY_CLEAR_TOP
is most often used in conjunction withFLAG_ACTIVITY_NEW_TASK
.When used together, these flags are a way of locating an existing activity inanother task and putting it in a position where it can respond to the intent.
Note: If the launch mode of the designatedactivity is"standard"
,it too is removed from the stack and a new instance is launched in its place tohandle the incoming intent. That's because a new instance is always created fora new intent when the launch mode is"standard"
.
Handling affinities
Theaffinity indicates which taskan activity prefers to belong to. By default, all the activities from the sameapplication have an affinity for each other. So, by default, all activities inthe same application prefer to be in the same task. However, you can modify thedefault affinity for an activity. Activities defined in different applicationscan share an affinity, or activities defined in the same application can beassigned different task affinities.
Youcan modify the affinity for any given activity with thetaskAffinity
attribute of the<activity>
element.
ThetaskAffinity
attribute takes a stringvalue, which must be unique from the default package name declared in the <manifest>
element, because the system uses thatname to identify the default task affinity for the application.
Theaffinity comes into play in two circumstances:
When the intent that launches anactivity contains theFLAG_ACTIVITY_NEW_TASK flag.
A new activity is, by default, launched into the task ofthe activity that calledstartActivity(). It's pushed onto the same backstack as the caller. However, if the intent passed to startActivity() contains the FLAG_ACTIVITY_NEW_TASK flag, the system looksfor a different task to house the new activity. Often, it's a new task.However, it doesn't have to be. If there's already an existing task with thesame affinity as the new activity, the activity is launched into that task. Ifnot, it begins a new task.
If this flag causes an activity to begin a new task andthe user presses theHome button toleave it, there must be some way for the user to navigate back to the task.Some entities (such as the notification manager) always start activities in anexternal task, never as part of their own, so they always putFLAG_ACTIVITY_NEW_TASK
in the intents they pass tostartActivity(). If you have an activity that can beinvoked by an external entity that might use this flag, take care that the userhas a independent way to get back to the task that's started, such as with alauncher icon (the root activity of the task has aCATEGORY_LAUNCHER intent filter; see theStarting a tasksection below).
When an activity has its allowTaskReparenting
attribute set to"true"
.
In this case, the activity can move from the task it startsto the task it has an affinity for, when that task comes to the foreground.
For example, suppose that an activity that reports weatherconditions in selected cities is defined as part of a travel application. Ithas the same affinity as other activities in the same application (the defaultapplication affinity) and it allows re-parenting with this attribute. When oneof your activities starts the weather reporter activity, it initially belongsto the same task as your activity. However, when the travel application's taskcomes to the foreground, the weather reporter activity is reassigned to thattask and displayed within it.
Tip: If an.apk
file contains more thanone "application" from the user's point of view, you probably want touse thetaskAffinity
attribute to assigndifferent affinities to the activities associated with each"application".
Clearing the back stack
Ifthe user leaves a task for a long time, the system clears the task of allactivities except the root activity. When the user returns to the task again,only the root activity is restored. The system behaves this way, because, afteran extended amount of time, users likely have abandoned what they were doingbefore and are returning to the task to begin something new.
Thereare some activity attributes that you can use to modify this behavior:alwaysRetainTaskState
If this attribute is set to "true"
in the root activity ofa task, the default behavior just described does not happen. The task retainsall activities in its stack even after a long period.
If this attribute is set to "true"
in the root activity ofa task, the stack is cleared down to the root activity whenever the user leavesthe task and returns to it. In other words, it's the opposite of alwaysRetainTaskState
.The user always returns to the task in its initial state, even after a leavingthe task for only a moment.
This attribute is like clearTaskOnLaunch
, but it operateson a single activity, not an entire task. It can also cause any activity to goaway, including the root activity. When it's set to"true"
, the activity remainspart of the task only for the current session. If the user leaves and thenreturns to the task, it is no longer present.
Starting a task
You can set up an activity as the entrypoint for a task by giving it an intent filter with"android.intent.action.MAIN"
as the specified action and"android.intent.category.LAUNCHER"
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>
Anintent filter of this kind causes an icon and label for the activity to bedisplayed in the application launcher, giving users a way to launch theactivity and to return to the task that it creates any time after it has beenlaunched.
Thissecond ability is important: Users must be able to leave a task and then comeback to it later using this activity launcher. For this reason, the twolaunch modes thatmark activities as always initiating a task,"singleTask"
and "singleInstance"
,should be used only when the activity has anACTION_MAINand aCATEGORY_LAUNCHERfilter. Imagine, for example, what could happen if the filter is missing: Anintent launches a "singleTask"
activity, initiating a new task, and the user spends some time working in thattask. The user then presses theHomebutton. The task is now sent to the background and is not visible. Now the userhas no way to return to the task, because it is not represented in theapplication launcher.
Forthose cases where you don't want the user to be able to return to an activity,set the<activity>element'sfinishOnTaskLaunch
to"true"
(see Clearing the stack).Furtherinformation about how tasks and activites are represented and managed in theoverview screen is available in Overview Screen.