Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> API翻譯 --- Tasks and Back Stack

API翻譯 --- Tasks and Back Stack

編輯:關於Android編程

IN THIS DOCUMENT

Saving Activity State 保存activity狀態

Managing Tasks 管理任務

Defining launch modes定義啟動模式

Handling affinities處理密切關系

Clearing the back stack清除回退棧

Starting a task啟動任務

ARTICLES

Multitasking the Android WayAndroid多任務方式

SEE ALSO

Android Design: Navigation Android設計:導航

manifest element 清單元素

An application usually contains multipleactivities. 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.

一個應用程序通常包含多個Activity。每一個activity被設計執行一定的行為跳轉到下個Activity。比如,一個email應用程序有一個顯示新郵件的列表的activity。當用戶選擇一個email的時候,一個新的activity被打開,顯示email的內容。

An activity can even start activities that exist in other applications on the device. 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. 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). When the email is sent, your activity resumes and it seems as if the email activity was part of your application. Even though the activities may be from different applications, Android maintains this seamless user experience by keeping both activities in the sametask.

一個Activity可以啟動其他應用程序的activity.比如,如果你的應用程序想發送一個email,你可以定義一個意圖去執行”發送“行為,其中可以包含一些數據,比如email的地址和信息。另一個應用程序的activity聲明處理這個意圖,並打開。這種情況下,目的是為了發送email,所以email應用程序組件的activity啟動(如果多個activity支持這個意圖,系統會讓用戶去選擇使用哪一個)。當email被發送,你的activity恢復並且email的activity好像是你的應用的一部分。盡管這些activity是來自不同的應用。Android通過保持不同的activity在相同的任務棧,讓用戶有無縫的體驗。

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.

當用戶為了某種工作,彼此之間相互作用的時候,任務棧是activity的集合。activity被分配到棧裡(回退棧),為了在棧裡每個activity能按照順序打開。

The device Home screen is the starting place for most tasks. 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. 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.

一個設備的主屏幕是任務棧的起點。當用戶觸摸應用程序啟動圖標(或主屏幕的快捷方式),應用程序的任務欄顯示前台。如果應用程序沒有任務棧(應用程序當前沒有被使用),新的任務棧會被創建,並且應用程序的主activity會打開,作為回退棧的根activity。

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 theBackbutton, 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). 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 theBackbutton. 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.

當前的actvity打開其他的activity,新的activity推出到回退棧的頂部並且獲得焦點。先前的回退棧保持在回退棧中,但是停止了。當這個activity停止的時候,系統保持著用戶的當前狀態。當用戶點擊回退按鈕的時候,當前的activity從棧的頂端彈出(這個activity被銷毀)並且先前的activity恢復(這個activity先前的界面被恢復)。activity在棧中不會被重新排列,只會從棧中壓入和彈出。當前的activity啟動時,壓入棧頂。當用戶離開點擊返回按鈕的時候彈出。這樣,回退棧操作按照”後進 先出“的對象結構。圖1 以時間線的方式展示了多個activity切換時對應當前時間點的回退棧情況。

Figure 1.A representation of how each new activity in a task adds an item to the back stack. When the user presses theBackbutton, the current activity is destroyed and the previous activity resumes.

圖1表示每一個新的activity都會添加到回退棧中。當用戶點擊返回按鈕,當前的activity被銷毀,先前的actvity恢復。

If the user continues to pressBack, 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.

如果用戶不停的按下返回鍵,那麼棧中的每一個回退棧都會被依次彈出,並顯示前一個activity,直到用戶回到home屏幕(或是啟動這個任務棧的activity)。當所有的activity都從回退棧中移除,這個棧也就不存在了。

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

任務棧B在前台接收用戶交互。棧A在後台等待恢復。

Figure 3.A single activity is instantiated multiple times.

一個activity被多次實例化。

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 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 focus while another task takes place, as shown in figure 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. The user presses theHomebutton, then starts a new application from the application launcher. When the Home screen appears, Task A goes into the background. When the new application starts, the system starts a task for that application (Task B) with its own stack of activities. After interacting with that application, the user returns Home again and selects the application that originally started Task 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. 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 selecting the app's task from therecent appsscreen). This is an example of multitasking on Android.

任務棧是一個完整的單位,當用戶啟動一個新的任務棧,或通過home按鈕返回到主界面的時候,這個任務棧移動到後台。當任務棧在後台的時候,裡面所有的activity都處在停止狀態。但是這個任務棧的回退棧仍然完整保留。-如圖2。當其他棧獲得焦點期間,當前棧只是失去焦點。任務棧可以回到焦點,以便用戶繼續之前的操作。例如,當前棧中有三個activity,--下面有兩個activity。這時,用戶按下home鍵,然後通過應用啟動一個新的activity。當主界面出現的時候,任務棧A進入後台。當新的應用啟動的時候,系統會重新啟動一個任務棧B。用完這個應用後,用戶再次返回主界面,並選中那個啟動任務棧A的應用。這時候,任務棧A進入了前台,--棧中的三個activty依然完好,位於最頂端的actvity恢復運行。這時候,用戶依然可以切換到任務棧B,通過返回主界面並選擇對象的圖標即可。(觸摸並按住home鍵調用最近的任務棧列表並選中)。這是多任務棧的實例。

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 aboutActivity state.

Android系統可以在後台保持多個任務棧。但是,如果用戶同時運行了多個後台任務棧,系統可能銷毀後台activity用於釋放內存。這種情況會導致activity狀態的丟失。閱讀actvity狀態的部分。

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 pushed 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. As such, if the user navigates backward using theBackbutton, each instance of the activity is revealed in the order they were opened (each with their 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 so is discussed in the later section aboutManaging Tasks.

因為回退棧中的activity永遠不會重新分配。如果應用允許某個Activiy可以啟動多次,則新的activity會壓入棧頂(而不是打開原來位於棧頂的activity)。於是,一個actvity可能初始化多次(甚至會位於不同的棧中)。如圖3 ,用戶使用返回鍵返回時,activity會按照原來打開的順序顯示出來。(用戶界面也會按照原來的狀態顯示)。如果你不希望你的activity被多次實例化,你可以改變的它。具體的方法在後面的管理多個任務棧部分講解。

To summarize the default behavior for activities and tasks:

總結activity和任務棧的默認行為:

  • When Activity A starts Activity B, Activity A is stopped, but the system retains its state (such as scroll position and text entered into forms). If the user presses theBackbutton while in Activity B, Activity A resumes with its state restored.

  • 當ActivityA啟動ActivityB的時候,ActvityA停止,但是系統保持它的狀態(比如滾動條位置和form中填出的文字)。如果用戶在actvityB界面中按返回鍵,Activity恢復它的儲存狀態。

  • When the user leaves a task by pressing theHomebutton, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack.

  • 當用戶按home按鈕離開任務棧的時候,當前的activity停止並且它的任務棧跳到後台。系統在任務棧中保存每一個actvity的狀態。如果用戶後使用選擇這個棧的圖標來恢復任務棧,任務棧就會回到前台,棧頂的activity會恢復進行。

  • If the user presses theBackbutton, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the systemdoes notretain the activity's state.

  • 如果用戶按下返回鍵,當前的activity從棧中彈出,並銷毀。棧中的前一個actvity恢復運行。當activity銷毀的時候,系統不會保存actvity的狀態。

  • Activities can be instantiated multiple times, even from other tasks.

  • activity可以在不同的時間,不同的的任務棧中被實例化多次。

Navigation Design 設計導航

For more about how app navigation works on Android, read Android Design'sNavigationguide.

應用導航如何工作的更多信息,閱讀Android設計導航的指導。

Saving Activity State 保存activity狀態


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—andshould—proactively retain the state of your activities using callback methods, in case the activity is destroyed and must be recreated.

如上所述,系統會在activity停止的時候保存其狀態。這樣,當用戶返回時,activity顯示的狀態和它停止時一樣。但是,你應該使用回調方法保存activity狀態。

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 theonSaveInstanceState()callback methods in your activity.

當系統停止Activity(比如啟動了一個新Activity或者Task 轉入後台),系統在需要回收內存的時候有可能會完全銷毀該Activity.這時,該Activity的狀態信息將會丟失.就算這種情況發生,該Activity仍然會存在於Back Stack中.但是當它回到棧頂時,系統將必須重建它(而不是恢復).為了避免用戶工作內容的丟失,你應通過實現Activity的onSaveInstanceState()來主動保存這些內容。

For more information about how to save your activity state, see theActivitiesdocument.

關於如何保存Activity 狀態的詳情,請參閱Activities 文檔。

Managing 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 except for the root activity when the user leaves the task.

如上所述,把所有已經啟動的Activity相繼放入同一個Task 中以及一個“後入先出”棧,Android 管理Task和Back Stack 的這種方式適用於大多數應用,你也不用去管理你的Activity 如何與Task關聯及如何彈出Back Stack .不過,有時候你或許決定要改變這種普通的運行方式.也許你想讓某個Activity啟動一個新的Task(而不是被放入當前Task中),或者你想讓 activity 啟動時只是調出已有的某個實例(而不是在Back Stack 頂創建一個新的實例)或者你想在用戶離開Task 時只保留根Activity,而Back Stack 中的其它 Activity 都要清空。

You can do these things and more, with attributes inthe

manifestelement and with flags in the intent that you pass to

startActivity().

你能做的事情還有很多,利用清單元素的屬性和傳入的

startActivity()intent 中的標識即可。

In this regard, the principalattributes you can use are:

這裡 ,你可以使用的屬性:

  • taskAffinity

  • launchMode

  • allowTaskReparenting

  • clearTaskOnLaunch

  • alwaysRetainTaskState

  • finishOnTaskLaunch

And the principal intent flags you can use are:

你可以用到intent的標識

  • FLAG_ACTIVITY_NEW_TASK

  • FLAG_ACTIVITY_CLEAR_TOP

  • FLAG_ACTIVITY_SINGLE_TOP

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 屬性和intent標識來定義Activity 與Task的關聯性,以及 Back Stack 的工作方式。

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 theBackbutton. Be sure to test for navigation behaviors that might conflict with the user's expected behavior.

大多數應用不應該改變Activity和Task默認的工作方式。 如果你確定有必要修改默認方式,請保持謹慎,並確保Activity的啟動和從其它Activity用返回鍵返回時的可用性.請確保對可能與用戶預期的導航方式相沖突的地方進行測試.

Defining launch modes 定義啟動模式

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:

啟動模式定義了一個新的Activity 實例與當前Task 的關聯方式。定義啟動模式的方法有兩種:

Using the manifest file使用清單文件

When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts.

當你在 manifest文件中聲明一個Activity 時,可以指定它啟動時與Task 的關聯方式。

Using Intent flags使用Intent標記

When you callstartActivity(), you can include a flag in theIntentthat declares how (or whether) the new activity should associate with the current task.

調用startActivity()時,可以在Intent中包含一個標識,用於指明新Activity 是如何(是否)與當前Task 相關聯。

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).

同樣的,如果ActivityA啟動了ActivityB,則 ActivityB可以在 manifest 中定義它如何與當之前的Task 關聯(如果存在的話),並且,ActivityA 也可以要求 ActivityB 與當前 task 的關聯關系.如果兩個Activity都定義了,則 Activity A的請求(intent 中定義)會比Activity B 的定義(在 manifest 中)優先.

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.

注意:manifest 文件中的某些啟動模式在 intent 標識中並不可用,反之亦然,intent 中的某些模式也無法在 manifest 中定義。

Using the manifest file 使用清單文件

When declaring an activity in your manifest file, you can specify how the activity should associate with a task using theelement'slaunchModeattribute.

在manifest文件中聲明Activity時,你可以使用元素的啟動模式屬性來指定Activity與Task的關系.

ThelaunchModeattribute specifies an instruction on how the activity should be launched into a task. There are four different launch modes you can assign to thelaunchModeattribute:

啟動模式屬性指明了Activity啟動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.

默認的,每次訪問實例化新的Activity,系統在啟動Activity的Task中創建一個新的Activity 實例,並且把 intent 傳送路徑指向它.該Activity 可以被實例化多次,各個實例可以屬於不同的Task,一個Task中也可以存在多個實例.

"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 itsonNewIntent()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 isnotan existing instance of the activity).

如果actvity在當前棧的頂部,該系統就會使用onNewIntent()方法通過intent 傳遞給已有實例。否則,還是實例化新的Activity.Activity可以被實例化多次,各個實例可以屬於不同的Task,一個Task中可以存在多個實例(但只有Back Stack的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). 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 receives the intent throughonNewIntent(), 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".

例如,假設Task的Back Stack中包含了根Activity A 和 Activities B、C、D(順序是 A-B-C-D; D在棧頂). 這時候傳過來的是啟動D的intent,如果D的啟動模式是默認的"standard",則會啟動一個新的實例,棧的內容就會變為 A-B-C-D-D.但是,如果 D 的啟動模式是"singleTop",則已有的D的實例會通過onNewIntent():接收這個 intent,因為該實例位於棧頂——棧中內容仍然維持 A-B-C-D 不變.當然,如果 intent 是要啟動 B 的,則 B 的一個新實例還是會加入棧中,即使 B 的啟動模式是"singleTop"也是如此.

Note:When a new instance of an activity is created, the user can press theBackbutton to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press theBackbutton to return to the state of the activity before the new intent arrived inonNewIntent().

注意:一個Activity的新實例創建完畢後,用戶可以按返回鍵返回前一個activity.但是當Activity 已有實例正在處理剛到達的intent 時,用戶無法用返回鍵回到onNewIntent()中 intent 到來之前的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 itsonNewIntent()method, rather than creating a new instance. Only one instance of the activity can exist at a time.

系統創建一個新的任務棧並保證activity在新棧的頂部。但是如果這個棧在不同的棧中,則系統會通過調用其實例的onNewIntent()方法把 intent傳給已有實例,而不是再創建一個新實例. 此 activity 同一時刻只能存在一個實例.

Note:Although the activity starts in a new task, theBackbutton still returns the user to the previous activity.

注意:雖然Activity啟動了一個新的Task,但用戶仍然可以用返回鍵返回前一個activity.

"singleInstance".

Same as"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.

除了系統不會把其它Activity 放入當前實例所在的 Task 之外,其它均與"singleTask"相同,Activity總是它所在Task 的唯一成員;它所啟動的任何Activity都會放入其它Task 中.

As another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying thesingleTasklaunch mode in theelement. This means that if your application issues an intent to open the Android Browser, its activity isnotplaced 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.

舉另一個事例:Android的浏覽器應用就是把web浏覽器Activity聲明為總是在它自己獨立的Task中打開——把activity設為singleTask模式.這意味著,如果你的應用提交 intent 來打開 Android 的浏覽器,則其 activity不會被放入你的應用所在的Task 中.取而代之的是,為浏覽器啟動一個新的Task;或是浏覽器已經在後台運行,只要把Task 重新調入前台來處理新 intent 就可以.

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, theBackbutton always takes the user to the previous activity. However, if you start an activity that specifies thesingleTasklaunch 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 includes all activities from the task brought forward, at the top of the stack. Figure 4 illustrates this type of scenario.

無論Activity是在一個新的Task中啟動,還是位於其它已經存在的Task中,用戶總是可以按返回鍵返回到前一個Activity 中.但是,如果你啟動了一個啟動模式設為singleTask的 activity,且有一個後台 task 中已存在實例的話,則這個後台 task 就會整個轉到前台.這時,當前的Back Stack就包含了這個轉入前台的Task 中所有的Activity,位置是在棧頂.圖4所描述的就是這一種場景

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.

圖 4.啟動模式為“singleTask”的Activity 如何加入Back Stack 的示意.如果Activity 已經是在後台 Task 中並帶有自己的Back Stack,則整個後台Back Stack 都會轉入前台,並放入當前Task 的棧頂.

For more information about using launch modes in the manifest file, see theelement documentation, where thelaunchModeattribute and the accepted values are discussed more.

在manifest 文件中使用啟動模式的詳情,請參閱元素文檔,其中詳細描述了launchMode屬性及其可用值.

Note:The behaviors that you specify for your activity with thelaunchModeattribute can be overridden by flags included with the intent that start your activity, as discussed in the next section.

注意:你使用啟動模式屬性為Activity設置的模式可以被啟動Activity的intent標識所覆蓋,這將在下一章節具體說明。

Using Intent flags使用 Intent 標識

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

tostartActivity().The flags you can use to modify the default behavior are:

在啟動Activity 時,你可以傳給startActivity()的 intent 中包含相應標識,用於修改Activity 與Task 的默認關系。這個標識可以修改的默認模式包括:

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 inonNewIntent().

在新的Task中啟動Activity.如果要啟動的Activity 已經運行於某個Task 中,則那個Task 將調入前台中,最後保存的狀態也將會恢復,Activity 將在onNewIntent()中接收到這個新 intent.

This produces the same behavior as the"singleTask"launchModevalue, discussed in the previous section.

這個模式與前一章節所描述述的"singleTask"啟動模式模式相同.

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 toonNewIntent(), instead of creating a new instance of the activity.

如果要啟動的Activity 就是當前Activity(位於Back Stack 頂),則已存在的實例將接收到一個onNewIntent()調用,而不是創建一個Activity 的新實例.

This produces the same behavior as the"singleTop"launchModevalue, discussed in the previous section.

這個模式與前一章節所述的"singleTop"啟動模式相同.

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), throughonNewIntent()).
如果要啟動的Activity已經在當前Task中運行,則不再啟動一個新的實例,且所有在其上面的Activity將被銷毀,然後通過onNewIntent()傳入 intent 並恢復Activity(不在棧頂)的運行.

There is no value for thelaunchModeattribute that produces this behavior.

此種模式在啟動模式中沒有對應的屬性值.

FLAG_ACTIVITY_CLEAR_TOPis most often used in conjunction withFLAG_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.

FLAG_ACTIVITY_CLEAR_TOP經常與FLAG_ACTIVITY_NEW_TASK結合起來一起使用.這些標識定位在其它Task中已存在的Activity,再把它放入可以響應 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".

注意:如果Activity 的啟動模式配置為"standard",它就會先被移除出棧,再創建一個新的實例來處理這個 intent,因為啟動模式為"standard"時,總是會創建一個新的實例。

Handling affinities 處理關系值

Theaffinityindicates which task an activity prefers to belong to. 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.

affinity指明Activity對於哪些Task親和力更高.默認情況下,同一個應用中的所有 activity 都擁有同一個affinity 值. 因此,在同一應用程序中的所有Acttivity都喜歡在相同的Task.不過,你可以修改Activity 默認的 affinity 值.不同應用中的Activity 可以共享同一個affinity 值,同一個應用中的Activity也可以賦予不同的Task affinity值.

You can modify the affinity for any given activity with thetaskAffinityattribute of theelement.

ThetaskAffinityattribute takes a string value, which must be unique from the default package name declared in theelement, because the system uses that name to identify the default task affinity for the application.

你可以使用元素

的任務親密度屬性修改Activity的affinity。任務親密度屬性是一個字符串值,必須與元素定義的包名稱保證唯一性,因為系統把這個包名稱用於標識應用的默認Task affinity值.

The affinity comes into play in two circumstances:

下面的兩種情況下affinity將會發揮作用:

When the intent that launches an activity contains theFLAG_ACTIVITY_NEW_TASKflag.

A new activity is, by default, launched into the task of the activity that calledstartActivity(). It's pushed onto the same back stack as the caller. However, if the intent passed tostartActivity()contains theFLAG_ACTIVITY_NEW_TASKflag, the system looks for 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 the same affinity as the new activity, the activity is launched into that task. If not, it begins a new task.

當啟動Activity的intent包含了FLAG_ACTIVITY_NEW_TASK標志.

默認情況下,一個新的Activity將被放入調用startActivity()的Activity 所在Task 中,且壓入調用者所在的Back Stack 頂棧.不過,如果傳給startActivity()的 intent 包含了FLAG_ACTIVITY_NEW_TASK標識,則系統會查找另一個Task並將新Activity 放入其中.一般情況下會新開一個任務,但並非一定如此.如果一個已有Task的affinity值與新 Activity的相同,則Activity 會放入該Task.如果沒有,則會新建一個新Task.

If this flag causes an activity to begin a new task and the user presses theHomebutton to leave 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 an external task, never as part of their own, so they always putFLAG_ACTIVITY_NEW_TASKin the intents they pass tostartActivity(). 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 aCATEGORY_LAUNCHERintent filter; see theStarting a tasksection below).

如果這個標志導致Activity啟動了一個新的Task並且用戶按下Home鍵離開時,必須采取某種方式讓用戶能回到此Task.某些應用(比如通知管理器)總是讓Activity放入其它Task 中啟動,而不是放入自己的Task 中,因此,它們總是把FLAG_ACTIVITY_NEW_TASK標識置入傳給startActivity()的 intent 中.如果你的Activity 可以被外部應用帶此標識來啟動,請注意用戶會用其它方式返回啟動Task,比如通過應用圖標(Task 的根 Activity 帶有一個CATEGORY_LAUNCHERintent過濾器;參閱下節#啟動task).

When an activity has itsallowTaskReparentingattribute set to"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. 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.

當一個Activity的allowTaskReparenting屬性設置為"true".

在這種情況下,當某個Task 進入前台時,Activity 的affinity 值又與其相同,則它可以從啟動時的Task 移入這個Task 中.例如:假設某旅游應用中有一個Activity 根據所選的城市來報告天氣情況.它的affinity 與同一應用中的其它Activity 一樣(整個應用默認的 affinity),且它允許重新指定此屬性的歸屬,當你的另一個Activity 啟動此天氣預告Activity 時,它會在同一個Task 中啟動.然而,當旅游應用的 Task 進入前台時,則天氣報告Activity將會重新放入其Task中並顯示出來.

Tip:If an.apkfile contains more than one "application" from the user's point of view, you probably want to use thetaskAffinityattribute to assign different affinities to the activities associated with each "application".

提示:如果一個.apk文件中包含了多個“application”,從用戶的角度來看,你可能想使用

taskAffinity屬性來分配每個“application”中 activity 的 affinity 值.

Clearing the back stack 清理回退棧

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.

如果用戶長時間離開某個Task,系統將會僅保留一個根Activity,而把其它Activity 都清除掉.當用戶返回Task 時,只有根Activity 會被恢復.系統的這種行為,是因為經過了很長時間後,用戶是要放棄之前進行的操作,返回Task 是為了開始新的操作.

There are some activity attributes that you can use to modify this behavior:

可以使用Activity的某些屬性來改變這種行為:

alwaysRetainTaskState

If this attribute is set to"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.

如果Task中根Activity 的此屬性設為"true",則默認的清理方式不會進行.即使過了很長一段時間,Task 中所有的Activity也還會保留在棧中.

clearTaskOnLaunch

If this attribute is set to"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. In other words, it's the opposite ofalwaysRetainTaskState. The user always returns to the task in its initial state, even after a leaving the task for only a moment.

如果Task 中根Activity 的此屬性設為"true",則只要用戶離開並再次返回該 Task,棧就會被清理至根Activity。也就是說,正好與alwaysRetainTaskState相反.用戶每次返回Task時看到的都是初始狀態,即使只是離開一會兒.

finishOnTaskLaunch

This attribute is likeclearTaskOnLaunch, but it operates on a single activity, not an entire task. It can also cause any activity to go away, including the root activity. When it's set to"true", the activity remains part of the task only for the current session. If the user leaves and then returns to the task, it is no longer present.

此屬性類似於clearTaskOnLaunch,只是它只對一個 Activity有效,不是整個Task.這能讓任何一個Activity 消失,包括 根Activity.如果Activity 的此屬性設為"true",則只會保留Task中當前session所涉及的內容.如果用戶離開後再返回Task,它就不存在.

Starting a task 啟動一個棧

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. For example:

你可以通過發送一個指定動作和類別為"android.intent.action.MAIN"、category 為"android.intent.category.LAUNCHER"發送 intent來指定某個Activity為Task的入口,例如:

 

 

 

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.

這種intent過濾器將會讓此Activity的圖標和標簽作為應用啟動圖標來顯示,用戶可以啟動此Activity,並且在之後任何時候返回其啟動時的Task.

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 twolaunch modesthat mark 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: An intent launches a"singleTask"activity, initiating a new task, and the user spends some time working in that task. The user then presses theHomebutton. The task is now sent to the background and is not visible. Now the user has no way to return to the task, because it is not represented in the application launcher.

第二個能力非常重要.用戶必須能離開一個Task ,之後能再回來使用這個啟動Task 的Activity.由於這個原因,Activity每次都會使用這兩種啟動模式"singleTask"和 ""singleInstance"啟動Task。並且僅用於ACTION_MAIN和CATEGORY_LAUNCHER過濾器的Activity才能使用.

如果缺少過濾器會發生怎樣的問題:某個intent啟動了一個"singleTask"activity。新建了一個Task,用戶在此Task中工作了一段時間.然後他按了Home鍵,Task 就轉入後台,變為不可見狀態,這時用戶就無法再回到Task了,因為它在應用啟動中沒有配置對應的屬性.

For those cases where you don't want the user to be able to return to an activity, set theelement'sfinishOnTaskLaunchto"true"

(seeClearing the stack).

對於那些你不希望用戶能夠返回的Activity,將元素的finishOnTaskLaunch設置為"true"即可.詳細請(參閱Clearing the stack)

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved