An application usually contains multiple activities. Each activity should be designed around a specific kind of action the user can perform and can start other activities. For example, an email application might have one activity to show a list of new email. When the user selects an email, a new activity opens to view that email.一個應用一般包括多個activities.每一個活動應該設計成用戶能執行的某一種動做,並能啓動其餘的活動。好比,一個email應用可能有一個顯示新的email 列表的活動,當用戶選擇一個email時,一個新的活動開啓顯示那個選擇的email.
html
An activity can even start activities that exist in other applications on the device.一個activity甚至能夠啓動該設備上的其餘應用中存在的活動。 For example, if your application wants to send an email, you can define an intent to perform a "send" action and include some data, such as an email address and a message. 好比,若是你的應用想要發送一個郵件,你能夠定義一個意圖,執行一個'send'動做,併發送一些數據,包括像出件地址和信息.An activity from another application that declares itself to handle this kind of intent then opens. 若是其餘應用的活動聲明瞭它能處理這種意思,那麼它將打開。In this case, the intent is to send an email, so an email application's "compose" activity starts (if multiple activities support the same intent, then the system lets the user select which one to use). 在這種狀況下,該意圖將發送一封email,所一個email應用的"compose(寫)"活動將啓動(若是多個活動支持該相同的意圖,那麼系統將讓用戶選擇使用那個).When the email is sent, your activity resumes and it seems as if the email activity was part of your application.當email被髮送,你的活動將恢復,而且看起來email的活動好像是你的應用的一部分。 Even though the activities may be from different applications, Android maintains this seamless user experience by keeping both activities in the same task.即便活動來自不一樣的應用,Android經過把這兩個活動保持在同一個任務中,以維持用戶的無縫(流暢)體驗.android
A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack (the "back stack"), in the order in which each activity is opened.當執行某項工做時,一個任務是用戶進行交互的活動的集合,爲了使每一個活動處理打開狀態,它們被按排在一個堆中(這個堆叫"返回堆").web
The device Home screen is the starting place for most tasks. 設備的Home屏幕是大多數任務開始的地方.When the user touches an icon in the application launcher (or a shortcut on the Home screen), that application's task comes to the foreground. 當用戶觸模應用運行器上的icon時(或者一個屏幕上的快捷方式),該應用的任務將出如今前景中.If no task exists for the application (the application has not been used recently), then a new task is created and the "main" activity for that application opens as the root activity in the stack.若是該應用沒有任務存在(該應用最近沒有使用),那麼一個新的任務將建立,而且該應用的"main"活動,將會打開,並看成根活動放在堆中.瀏覽器
When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. 噹噹前活動啓動了另外一個活動,那個新的活動被壓入到堆的頂端,而且獲取聚焦。The previous activity remains in the stack, but is stopped.先前的那個活動仍駐留在堆中,可是它中止了。 When an activity stops, the system retains the current state of its user interface.當一個活動中止時,系統會記住它的用戶界面的當前狀態。 When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored). 當用戶按BACK鍵時,堆頂的當前活動被彈出(該活動被銷燬),而且前一個活動恢復(前一個活動的UI狀態被恢復)Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the BACK key. 在堆中的活動不會被從新按排,只有壓入及從堆中彈出--被當前活動啓動時壓入到堆,而且當用戶用back離開它時,被彈出.As such, the back stack operates as a "last in, first out" object structure.所以,返回堆的操做是"後進,先出"的對象結構. Figure 1 visualizes this behavior with a timeline showing the progress between activities along with the current back stack at each point in time.圖1以時間線,量化了這個行爲,顯示了活動與當前堆在相應的時間上的每一個點的過程.session
If the user continues to press BACK, then each activity in the stack is popped off to reveal the previous one, until the user returns to the 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.若是用戶繼續按BACK,那麼堆中的每一個活動將被彈出以顯示前一個活動,直到用戶返回到HOME屏幕(或者返回到該任務開始時正在運行的活動).當全部的活動從堆中移出,任務再也不存在.app
A task is a cohesive unit that can move to the "background" when users begin a new task or go to the Home screen, via the HOME key.一個任務是一個緊密的單元,當用戶開始一個新的任務或者去HOME 屏幕時,它能夠被移到"後臺" 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 focus while another task takes place, as shown in figure 2.當處理後臺時,全部堆中的活動被中止了,但返回堆爲任務保持它的原樣---該任務只是簡單的失去了,其餘任務佔據的焦點而已,如圖2所示. A task can then return to the "foreground" so users can pick up where they left off. 一個任務而後能夠返回到前臺,因此用戶能獲得他們離開的地方。Suppose, for example, that the current task (Task A) has three activities in its stack—two under the current activity. 假設,有一個例子,當前任務(任務A),在它的堆中有三個任務,其中兩個處於當前的活動之下.The user presses the HOME key, then starts a new application from the application launcher. When the Home screen appears, Task A goes into the background. 用戶按了HOME鍵,而後從應用運行器開始了新的應用,當主屏幕出現時,任務A進入到了後臺。When the new application starts, the system starts a task for that application (Task B) with its own stack of activities.當應用開始時,系統爲應用(任務B)開啓一個任務,該應用捅有本身的活動的堆. After interacting with that application, the user returns Home again and selects the application that originally started Task A. 當與該應用交互完後,用戶再次返回到Home,並選擇了原來已開啓了任務A的應用.Now, Task A comes to the foreground—all three activities in its stack are intact and the activity at the top of the stack resumes.如今,任務A出如今前景---全部該任務堆中的三個活動仍是原樣,而且堆頂的活動恢復. At this point, the user can also switch back to Task B by going Home and selecting the application icon that started that task (or by touching and holding the HOME key to reveal recent tasks and selecting one). 在這個時候,用戶能夠經過進入Home,並選擇應用icon,開啓那個任務,進入到任務B(或者經過觸模並按住HOME鍵,顯示最近的任務,並選擇一個).This is an example of multitasking on Android.這是一個在Android上的多任務的例子.less
Note: Multiple tasks can be held in the background at once. However, if the user is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost. See the following section about Activity state.後臺能夠同時保持多個任務,然而,若是用戶同時運行多個後臺任務,那麼系統可能爲了回收內存而開始刪除後臺活動,從而致使活動狀態被丟失,關於活動狀態Activity state請看後面的部分ide
Because the activities in the back stack are never rearranged, if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and popped onto the stack (rather than bringing any previous instance of the activity to the top).由於返回堆中的活動再不會被從新按排。若是你的應用容許用戶從多個活動啓動某個特別的活動,該活的新實例將被建立,並壓入到堆頂(而不是把該活動以前的任意一個實例帶入到堆頂) As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. 所以,如圖3所示,你應用中的某個活動可能被屢次實例化.As such, if the user navigates backward using the BACK key, each instance of the activity is revealed in the order they were opened (each with their own UI state). 也正由於如此,若是用戶用BACK鍵導航返回,爲了能打它們,該活動的每次實例化將被顯示.However, you can modify this behavior if you do not want an activity to be instantiated more than once. How to do so is discussed in the later section about Managing Tasks.但,若是你不想某個活動被屢次實例化,你能夠修改這個行爲。怎樣去作,在後面Managing Tasks關於管理任務一節討論.測試
To summarize the default behavior for activities and tasks:總結任務和活動的默認行爲:
As discussed above, the system's default behavior preserves the state of an activity when it is stopped.如上所述,當活動停此時,系統默認是保存活動的狀態的。 This way, when users navigate back to a previous activity, its user interface appears the way they left it.經過這種方式,當用戶導航返回時,它有用戶界面與它離開時顯示的同樣。 However, you can—and should—proactively retain the state of your activities using callback methods, in case the activity is destroyed and must be recreated.然而,你能夠而且能夠用回調方法提早保存活動的狀態,一旦活動被銷燬,就必須得從新建立.
When the system stops one of your activities (such as when a new activity starts or the task moves to the background), the system might destroy that activity completely if it needs to recover system memory. 當系統中止你的一個活動時(好比當一個新的活動啓動或者任務被移到後臺),若是須要恢復系統內存,系統可能把你的活動徹底銷燬了。When this happens, information about the activity state is lost.當發生這種狀況時,關於活動的狀態信息全會丟失。 If this happens, the system still knows that the activity has a place in the back stack, but when the activity is brought to the top of the stack the system must recreate it (rather than resume it). 若是是這種狀況:系統知道該活動在返回堆中的某個位置,可是當要把該活動帶到堆頂時,系統必須從新建立它(而不是恢復它)In order to avoid losing the user's work, you should proactively retain it by implementing the onSaveInstanceState()
callback methods in your activity.爲了不丟失用戶的工做。你應經過實現onSaveInstanceState()
方法,提早保存它。
For more information about how to save your activity state, see the Activities document.更多關於如何保存活動的狀態,請看Activities文檔
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. Android管理任理任務和返回堆的方法如上所述---經過把全部連續啓動的活動放在一個任務中,並放入後進先出的堆中---對於許多應用來講工做得很好,而且你不須要擔憂它們是怎樣與任務連聯起來的及在堆中是怎能樣存在的.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 except for the root activity when the user leaves the task.或者,當用戶離開任務時,你想要清除你的返回堆中除根活動以外的全部活動.
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()
.經過在manifest文中使用<activity>元素的屬性和你傳給
的intent標誌,你能夠作到這些及更其餘的事情.
startActivity()
In this regard, the the principal <activity>
attributes you can use are:就這點來講,你能用的<activity>
最首要的屬性是
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch
And the principal intent flags you can use are:及最主要的意圖標誌是:
In the following sections, 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.在接下來的部分,你將明白如何使用這些manifest屬性和意圖標誌來定義,活動怎樣與任務關聯及它們在返回堆中的行爲。
Caution: Most applications should not interrupt the default behavior for activities and tasks. If you determine that it's necessary for your activity to modify the default behaviors, use caution and be sure to test the usability of the activity during launch and when navigating back to it from other activities and tasks with the BACK key. Be sure to test for navigation behaviors that might conflict with the user's expected behavior.注意大多數應用不須要中斷活動和任務的默認行爲。若是你決定你的活動有必要改變默認的行爲,要當心使用,而且確保測試活動運行期間的可用性,當用BACK鍵從其餘任務及活動返回時的狀況。並確保測試導航行爲是否可能與用戶所指望的行爲相沖突。
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:運行模式定義了一個活動的新的實例是如何與當前任務相關聯的。你能夠兩種方法,定義不一樣的運行模式
When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts.當你在manifest文件中聲明活動時,你能夠指定該活動開始時,如何與任務關聯。
When you call startActivity()
, you can include a flag in the Intent
that declares how (or whether) the new activity should associate with the current task.當你調用startActivity(),你能夠在中包括一個標誌,聲明怎樣(或者是否)新的活動應該與當前任務相關聯。
As such, if Activity A starts Activity B, Activity B can define in its manifest how it should associate with the current task (if at all) and Activity A can also request how Activity B should associate with current task. If both activities define how Activity B should 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).所以,若是活動A啓動活動B,活動B能夠在它的manifest中定義它是怎樣與當前任務相關聯的(或根不本),而且活動A也能夠請求,活動B 應該與當前任務相關聯。若是兩個活動都定義了活動B 該怎樣與當前任務相關聯,那麼活動A的請求(定義在乎圖中)優先於活動B的請求(定義在manifest中)
Note: Some the launch modes available in the manifest 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.注意,一些運行模式能夠在manifest 中使用,但不能看成一個意圖標誌使用,同理,一些運行模式能夠看成意圖標記使用,但不能在manifest文件中定義.
When declaring an activity in your manifest file, you can specify how the activity should associate with a task using the <activity>
element's launchMode
attribute.當在你的manifest文件中聲明一個活動時,你能夠用<activity>
元素的launchMode
屬性,指定這個活動怎樣與任務相關聯.
The launchMode
attribute specifies an instruction on how the activity should be launched into a task. There are four different launch modes you can assign to the launchMode
attribute:launchMode
該屬性發出一個指令,指示活動應該怎麼在任務中運行.你能夠用launchMode
該屬性指定4種不一樣的運行模式.
"standard"
(the default mode)標準(默認模式)
"singleTop"單個置頂模式
onNewIntent()
method, rather than creating a new instance of the activity.若是活動的一個實例存在於當前任務的頂部,則系統通調用它的
onNewIntent()
方法路由意圖給該實例,而不是建立該活動的新實例. 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 the activity at the top of the back stack is
not an existing instance of the activity).該活動能夠被實例化屢次,每次屬於不一樣的任務,並一個任務能夠有多個實例(可是隻有當返回堆頂的活動,不存在該活動的實例的狀況下)
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). 好比,假設一個任務的返回堆由根活動A及活動B,C,D構成.(這個堆是A-B-C-D;D是置頂)An intent arrives for an activity of type D. 一個意圖要路由給活動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.若是D是默認運行模式"standard
",則該類的新實例被運行,而且堆就變了A-B-C-D-D. However, if D's launch mode is "singleTop"
, the existing instance of D receives the intent through onNewIntent()
, because it's at the top of the stack—the stack remains A-B-C-D.然而,若是D的運行模式是"singleTop
",則D存在的實例經過方法接收意圖,由於它是在堆頂----該堆仍然保持是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"
.但,若是活動B的意圖到了,那麼即便B的運行模式是"singleTop
"模式,仍會將B的新實例加入到堆上.
Note: When a new instance of an activity is created, the user can press the BACK key to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the BACK key to return to the state of the activity before the new intent arrived in onNewIntent()
.注意當一個活動的新實例被建立時,用戶可用BACK鍵返回到先前的活動.但當一個活動已存在的實例處理了一個新意圖時,用戶不能按BACK鍵返回到該新意圖到來以前該活動的狀態.
"singleTask"單任務
onNewIntent()
method, rather than creating a new instance. 然而,若是活動的該活動的一個實例已存在於一個獨立的任務中,系統經過調用它的
onNewIntent()
方法把意圖路由給存在的實例.Only one instance of the activity can exist at a time.該活動一次只能存在一個實例.
Note: Although the activity starts in a new task, the BACK key still returns the user to the previous activity.雖然該活動在一個新的任務開啓的,但BACK仍返回給用戶一個以前的活動
"singleInstance"
.單實例
"singleTask"
, except 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.相似於"
singleTask
",指望系統不運行任何其餘活動進入到,持有該活動的實例的任務中。該活動老是它的任務的單個惟一成員.任何由此活動開啓的活動,都在一個獨立的任務中開啓.
As another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying the singleTask
launch mode in the <activity>
element.另外一個例子,Android的瀏覽器應用,聲明瀏覽器的活動老是應該在它本身的任務中啓動---經過在<activity>
元素中指定"singleTask
"運行模式 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.這表示,若是你的應用發出一個打開該Android瀏覽器的意圖,你的應用的活動不是處於跟你的應用所處的相同的任務中. 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. 而是,要麼爲瀏覽器開啓一個新的任務,或者,若是瀏覽器已經有一個任務在後臺運行,則該任務將帶到前臺處理該新的意圖.
Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the BACK key always takes the user to the previous activity. BACK鍵老是把前一個活動帶給用戶,而無論該活動是不是在一個新任務中開啓的,或者在同一個任務中開啓的.However, if you start an activity from your task (Task A) that specifies the singleTask
launch mode, then that activity might have an instance in the background that belongs to a task with its own back stack (Task B).然而,若是你在你的任務A中開啓了一個活動,而該活動指定了"singleTask
"運行模式,而且該活動在其所屬的捅有返回堆的任務B中,有一個實例在後臺運行. In this case, when Task B is brought forward to handle a new intent, the BACK key first navigates backward through the activities in Task B before returning to the top-most activity in Task A. 在這種狀況,當任務B進入前臺處理該新的意圖,BACK鍵在返回到任務A的頂部活動以前,首先導航返回到任務B的活動.Figure 4 visualizes this type of scenario.圖4可視了該情景.
For more information about using launch modes in the manifest file, see the <activity>
element documentation, where the launchMode
attribute and the accepted values are discussed more.更多關於在manifest文件中使用運行模式的信息,看<activity>
元素文檔,那裏有更多關於該launchMode
屬性及其可接受的值的討論.
Note: The behaviors that you specify for your activity with the launchMode
attribute can be overridden by flags included with the intent that start your activity, as discussed in the next section.注意,你經過該屬性爲你活動指定的行爲,能夠被開啓你活動的活動,所帶有的意圖的標誌所覆蓋,在下面討論.
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()
. 當開啓一個活動時,你能夠在傳給startActivity()
方法的意圖中包含標誌,修改活動與它的任務的默認關係。The flags you can use to modify the default behavior are:你能夠用來修改默認行爲的標誌有:
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 receives the new intent in onNewIntent()
.在一個新的任務中啓動一個活動,若是你要啓動的這個活動,已在一個任務中運行着,那麼任務會被帶到前臺,並恢復它的最近的狀態.而且該活動在它的onNewIntent()
方法中接收新的意圖.
This produces the same behavior as the "singleTask"
launchMode
value, discussed in the previous section.這和前面討論的「singleTask
」單任務運行模式launchMode
值的行爲表現是同樣的。
FLAG_ACTIVITY_SINGLE_TOP
If the activity being started is the current activity (at the top of the back stack), then the existing instance receives a call to onNewIntent()
, instead of creating a new instance of the activity.若是該開啓的活動是當前活動(即在返回堆頂),那麼該存在的實例接收onNewIntent()
方法的調用.而不是建立該活動的新實例。(來接收onNewIntent()
的調用)
This produces the same behavior as the "singleTop"
launchMode
value, discussed in the previous section.這和前面討論的單個置頂"singleTop
"運行模式launchMode
值的行爲表現是同樣的.
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()
).若是該活動已在當前任務中運行,調用onNewIntent()
方法時,全部該活運之上的其餘活動所有被銷燬,而且這個意圖傳遞給該活動恢復的實例.而不是運行該活動的新實例.
There is no value for the launchMode
attribute that produces this behavior.沒有一個launchMode
運行模式屬性值,能致使這個行爲.
FLAG_ACTIVITY_CLEAR_TOP
is most often used in conjunction 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.該標誌常常與該標誌一塊兒結合使用,當他們一塊兒使用時,這些標誌是定位其餘任務中存在的活動,並把它放到某個能響應該意圖的位置的一個方法.
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"
.注意,若是該指定活動的運行模式是"standard".它也會被從堆移除,而且在它的位置運行一個新的實例來處理即將到來的意圖。這是由於當運行模式是"standard"的時,老是會從一個新意圖建立一個新實例。
The affinity indicates which task an activity prefers to belong to. affinity 是指某個活動屬於那個任務。By default, all the activities from the same application have an affinity for each other.默認狀況下,全部來自同一個應用的活動,互相之間捅有一個親密度關係. So, by default, all activities in the same application prefer to be in the same task.因此,默認狀況下,同一個應用的全部活動更願意屬於同一個任務. However, you can modify the default affinity for an activity. Activities defined in different applications can share an affinity, or activities defined in the same application can be assigned different task affinities.然而,你能夠修改活動的默認親密度關係,不一樣應用的多個活動能夠共享同一個親密度關係,或者同一個應用中定義的多個活動能夠分派不一樣任務的親密度關係.
You can modify the affinity for any given activity with the taskAffinity
attribute of the <activity>
element.你能夠經過<activity>
元素的taskAffinity
屬性修改指定活動的親密度關係.
The taskAffinity
attribute takes a string value, which must be unique from the default package name declared in the <manifest>
element, because the system uses that name to identify the default task affinity for the application.這個屬性是一個字串值,它必須是在元素中聲明的默認包名中是惟一的(大概意思是惟一的值),由於系統將用這個名字爲應用指定默認任務的親密度關係.
The affinity comes into play in two circumstances:這個親密度,來源於兩種不一樣玩境的角色:
FLAG_ACTIVITY_NEW_TASK
flag.當意圖運行的某個活動包含FLAG_ACTIVITY_NEW_TASK
標誌時. A new activity is, by default, launched into the task of the activity that called startActivity()
.一般,一個新的活動會運行在調用該startActivity()
方法的活動所在的任務中。 It's pushed onto the same back stack as the caller. 它被壓入到與調用者相同的返回堆頂。 However, if the intent passed to startActivity()
contains the FLAG_ACTIVITY_NEW_TASK
flag, the system looks for a different task to house the new activity.然而,若傳給startActivity()
方法的意圖,包含一個FLAG_ACTIVITY_NEW_TASK
標誌,系統將尋找一個不一樣的任務,宿主該新活動。Often, it's a new task.一般,它是一個新活動. However, it doesn't have to be.但,這不是必須的。 If there's already an existing task with the same affinity as the new activity, the activity is launched into that task. If not, it begins a new task.若是已有一個存在的任務與這個新活動有同一個親密度關係,該活動將在那個任務中運行。若是沒有,則會開啓一個新的任務.
If this flag causes an activity to begin 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.若是這個標誌致使某個活動開啓了一個新的任務,而且用戶按HOME鍵離開了它,那麼必須有一些方法可讓用戶能導航返回到該任務。 Some entities (such as the notification manager) always start activities in an external task, never as part of their own, so they always put FLAG_ACTIVITY_NEW_TASK
in the intents they pass to startActivity()
.一些實體(好比通知管理器),老是在另外一個任務中開啓新活動的,從不會將把新的活動當成它本身的一部分,因此他們老是在傳給startActivity()
該方法的意圖中包含這個標誌. If you have an activity that can be invoked by an external entity that might use this flag, take care that the user has a independent way to get back to the task that's started, such as with a launcher icon (the root activity of the task has a CATEGORY_LAUNCHER
intent filter; see the Starting a task section below).若是你有一個活動,能夠被外部實體使用這個標誌調用,那麼要注意,能讓用戶有一個獨立的途徑返回到該啓動的任務。好比運行器圖標(任務的根活動有一個CATEGORY_LAUNCHER
意圖過濾器.參考下面的Starting a task部分)
allowTaskReparenting
attribute set to "true"
.當一個活動的allowTaskReparenting
屬性設爲"true
"真時. In this case, the activity can move from the task it starts to [很差翻譯,在這裏斷一下句] the task it has an affinity for, when that task comes to the foreground.在這種狀況下,當那個任務進入到前臺時,任務能夠從他開啓的,並與其有親密度關係的任務上移走.
For example, suppose that an activity that reports weather conditions in selected cities is defined as part of a travel application. 好比,假設一個報道某個選定城市天氣情況的活動,定義爲一個旅遊應用的一部分。 It has the same affinity as other activities in the same application (the default application affinity) and it allows re-parenting with this attribute.它與同一個應用中的其餘活動捅有同一個親密度關係(默認應用親密度)而且,它容許了re-parenting(即容許把本身改變到其餘的任務中)這個屬性。 When one of your activities starts the weather reporter activity, it initially belongs to the same task as your activity. 當你的一個活動啓動了報道天氣情況的這個活動時,它一開始與你的活動屬於同一個任務。However, when the travel application's task comes to the foreground, the weather reporter activity is reassigned to that task and displayed within it.然而,當旅遊應用的任務進入到前臺,該報道天氣情況的活動,被從新指派到該旅遊應用的任務,並與它一塊兒顯示.
Tip: If an .apk
file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity
attribute to assign different affinities to the activities associated with each "application".若是從用戶的角度來看,一個.apk文件包括不此於一個"應用applicaion",那麼你可能想用taskAffinity
這個屬性,爲與該活動相關聯的每一個"應用applicaion",指定不一樣的親密度關係。
If the user leaves a task for a long time, the system clears the task of all activities 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, users likely have abandoned what they were doing before and are returning to the task to begin something new.系統有這樣的行爲,是由於,在一段長期的時間後,用戶可能放棄了他之前正在作後情,而且當再次要返回到該任務時,是要作一些新的事情。
There are some activity attributes that you can use to modify this behavior:你能夠用這些活動屬情修改這個行爲。
alwaysRetainTaskState老是記住任務狀態
"true"
in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period.若是任務的根活動的這個屬性被置爲"true",上面描述的默認行爲將不會發生。任務會記住它的堆中的全部活動,那怕離開了很長一段時間.
clearTaskOnLaunch運行時清除堆
"true"
in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it. 若是任務的根活動的這個屬性是"
true
",任什麼時候候用戶離開了該任務並返回到該任務時,堆會被清除到根活動。 In other words, it's the opposite of
alwaysRetainTaskState
. The user always returns to the task in its initial state, even after a leaving the task for only a moment.換句話說,就是它與
alwaysRetainTaskState
屬性相反.用戶老是返回到該任務的初始狀態,那怕離開了該任務一少會.
finishOnTaskLaunch
clearTaskOnLaunch
, but it operates on a single activity, not an entire task. 這個屬性與
clearTaskOnLaunch
屬性相似。便它的操做僅對於單個活動,而不是整個堆。It can also cause any activity to go away, including the root activity. 它能夠致使包括根活動在內的全部活動,離開(go away).When it's set to
"true"
, the activity remains part of the task only for the current session.當它設爲"true"時,(僅用於當前會話的任務中的部分活動保留) If the user leaves and then returns to the task, it is no longer present.若是用戶離開並返回到該任務,它再也不出現.
You can set up an activity as the entry point 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. 你能夠把一個活動設置爲某個任務的整個入點,經過使用意圖過濾器android.intent.action.MAIN
指定的動做和用android.intent.category.LAUNCHER
指定分類.For example:例如
<activity ... >
<intent-filter ... >
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
...
</activity>
An intent filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way to launch the activity and to return to the task that it creates any time after it has been launched.這種意圖會致使在應用運行器上顯示一個該活動的圖標和標籤,給了用戶運行該活動和在該活動運行後,在任什麼時候候建立它來返回到該任務的一條途徑.
This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher. 第二個功能很是重要:用戶必須可以使用這個活動運行器,在離開該任務後可以返回到該任務. For this reason, the two launch modes that mark activities as always initiating a task, "singleTask"
and ""singleInstance"
, should be used only when the activity has an ACTION_MAIN
and a CATEGORY_LAUNCHER
filter.基於這個緣由,"singleTask
"和"singleInstance
"兩個運行模式,應該用於有ACTION_MAIN
和CATEGORY_LAUNCHER
過濾器的活動 .Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask"
activity, initiating a new task, and the user spends some time working in that task. The user then presses the HOME key. 想像一下,若是丟失了該過濾 器會發生什麼:一個意圖運行了"singleTask
"活動,初始化一個新任務,而且用戶在該任務上處理了一些事情,而後按了HOME鍵. The task is now sent to the background and not visible.如今該任務被髮送到後臺,而且不可見了。 Because it is not represented in the application launcher, the user has no way to return to the task.因爲該任務沒有顯示在應用運行器中,因此用戶沒有辦法再返回該任務。
For those cases where you don't want the user to be able to return to an activity, set the <activity>
element's finishOnTaskLaunch
to "true"
(see Clearing the stack).若是你碰到不想讓用戶返回到某個活動的狀況,設置<activity>
該元素的finishOnTaskLaunch
該屬性爲"true"(參考"清除堆那裏Clearing the stack")