由于本人英文能力實(shí)在有限,不足之初敬請(qǐng)諒解
本博客只要沒(méi)有注明“轉(zhuǎn)”,那么均為原創(chuàng),轉(zhuǎn)貼請(qǐng)注明鏈接
android task與back stack 開(kāi)發(fā)文檔翻譯 - 1
android task與back stack 開(kāi)發(fā)文檔翻譯 - 2
android task與back stack 開(kāi)發(fā)文檔翻譯 - 3
?
Task和Back Stack
?
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.
一個(gè)應(yīng)用通常包含多個(gè)activity
每個(gè)activity都應(yīng)該被設(shè)計(jì)成圍繞著一種用戶(hù)可執(zhí)行并且能打開(kāi)其他activity明確的action
例如:一個(gè)郵件應(yīng)用也許有一個(gè)activity用來(lái)顯示新郵件列表
當(dāng)用戶(hù)選中一封郵件時(shí),一個(gè)新activity被打開(kāi),用來(lái)查看這封郵件。
?
?
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 same task.
一個(gè)activity甚至可以打開(kāi)設(shè)備中存在于其他應(yīng)用的activity
例如:如果你的應(yīng)用想發(fā)送一封郵件,你可以定義一個(gè)intent用來(lái)執(zhí)行一個(gè)發(fā)送的action并且包含一些數(shù)據(jù),比如郵件地址和信息
其他應(yīng)用中,聲明它自己可以處理這樣的intent的activity會(huì)打開(kāi)。
這個(gè)例子中,intent用來(lái)發(fā)送郵件,所以一個(gè)郵件程序的"compose" activity會(huì)啟動(dòng)(如果多個(gè)activity都支持這個(gè)intent,那么系統(tǒng)會(huì)讓用戶(hù)選擇使用哪一個(gè))
當(dāng)郵件發(fā)送完畢,你的activity會(huì)resume并且郵件的activity看上去好像你的應(yīng)用的一部分
雖然activity可能來(lái)自不同的應(yīng)用,Android通過(guò)保持兩個(gè)應(yīng)用的activity在同一個(gè)task中來(lái)保持這種無(wú)縫的用戶(hù)體驗(yàn)。
?
?
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.
一個(gè)task是用戶(hù)執(zhí)行一個(gè)任務(wù)時(shí),與用戶(hù)交互的activity的一個(gè)集合
這些activity按著被打開(kāi)的順序被整理到一個(gè)stack中(back stack)
?
?
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.
設(shè)備的Home頁(yè)面是大多數(shù)task的起始位置。
當(dāng)用戶(hù)觸摸一個(gè)在launcher應(yīng)用中的圖標(biāo)(或者Home頁(yè)面中的快捷方式)時(shí),應(yīng)用的task就會(huì)來(lái)到前臺(tái)。
如果這個(gè)應(yīng)用還沒(méi)有task(這個(gè)應(yīng)用最近沒(méi)有被使用過(guò)),那么一個(gè)新的task會(huì)被建立并且應(yīng)用的“主”activity作為stack的根activity被打開(kāi)。
?
?
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 button, 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 the Back button.?
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.
當(dāng)當(dāng)前activity打開(kāi)另一個(gè)activity時(shí),新的activity會(huì)被推入stack的頂部并且獲得焦點(diǎn)
前一個(gè)activity仍然在stack中,但是處于stopped狀態(tài)
當(dāng)一個(gè)activity處于stop狀態(tài)時(shí),系統(tǒng)會(huì)保持它當(dāng)前的UI狀態(tài)
當(dāng)用戶(hù)按back鍵時(shí),當(dāng)前的activity從stack頂部彈出(activity會(huì)被銷(xiāo)毀)并且上一個(gè)activity變成resume狀態(tài)(上一個(gè)UI狀態(tài)被恢復(fù))
stack中的activity絕不會(huì)重新排列,在stack中只有壓入和彈出 —— 當(dāng)被當(dāng)前的activity啟動(dòng)時(shí)壓入stack,當(dāng)用戶(hù)使用back鍵離開(kāi)它時(shí)彈出。
就其本身而言,back stack操作是像“后進(jìn)先出”的對(duì)象結(jié)構(gòu)一樣
下圖通過(guò)程序在多個(gè)activity之間連同當(dāng)前的back stack適時(shí)地在每一個(gè)點(diǎn)的時(shí)間線(xiàn)展示了這種行為。
Figure 1. A representation of how each new activity in a task adds an item to the back stack.?
When the user presses the Back button, the current activity is destroyed and the previous activity resumes.
上圖為task中每一個(gè)新activity如何向添加一個(gè)條目到back stack的展示。
當(dāng)用戶(hù)按下back鍵時(shí),當(dāng)前的activity被銷(xiāo)毀并且恢復(fù)上一個(gè)activity。
?
?
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.
如果用戶(hù)繼續(xù)按back鍵,那么每一個(gè)在stack中的activity都會(huì)彈出暴露出上一個(gè)activity,直到用戶(hù)返回到Home(或者退回到task開(kāi)始時(shí)運(yùn)行的activity)
當(dāng)所有activity都被從stack中移除之后,task也就不復(fù)存在了。
?
?
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 button.?
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 the Home button, 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 touching and holding the Home button to reveal recent tasks and selecting one).?
This is an example of multitasking on Android.
task是一個(gè)緊密結(jié)合的單元,當(dāng)用戶(hù)打開(kāi)一個(gè)新的task或者瀏覽Home主屏?xí)r,task可以移動(dòng)到“后臺(tái)”
當(dāng)在后臺(tái)時(shí),task中所有的activity都是stopped狀態(tài),但是task的back stack保持完整,當(dāng)另一個(gè)task占據(jù)位置時(shí)之前的task只是簡(jiǎn)單的失去焦點(diǎn),如上圖所示。
task可以返回到前臺(tái),所以用戶(hù)可以回到他們離開(kāi)的地方。
假設(shè):例如當(dāng)前的task(task A)在他的stack中有3個(gè)activity,其中兩個(gè)在當(dāng)前activity下面。
用戶(hù)按下Home鍵,然后從launcher應(yīng)用啟動(dòng)一個(gè)新的應(yīng)用。
當(dāng)home頁(yè)面出現(xiàn)的時(shí)候,task進(jìn)入后臺(tái)。
當(dāng)新的應(yīng)用開(kāi)啟時(shí),系統(tǒng)為這個(gè)新應(yīng)用自己的activity的stack開(kāi)啟一個(gè)task(Task B)
與新應(yīng)用交互完成之后,用戶(hù)返回Home然后選擇啟動(dòng)task A的原始應(yīng)用。
現(xiàn)在,Task A來(lái)到前臺(tái),stack中的3個(gè)activity原封不動(dòng)沒(méi)有改變,stack最頂部的activity重新開(kāi)始。
這時(shí)候,用戶(hù)可以通過(guò)去Home選擇啟動(dòng)Task B的應(yīng)用圖標(biāo)轉(zhuǎn)換回到Task B(或者通過(guò)長(zhǎng)按Home鍵顯示出最近的task,然后選擇)
這是一個(gè)android上面的多任務(wù)的例子。
?
?
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.
注意:多task可以同時(shí)進(jìn)入后臺(tái)。
然而,如果用戶(hù)同時(shí)運(yùn)行了很多task在后臺(tái),系統(tǒng)也許為了恢復(fù)內(nèi)存而開(kāi)始銷(xiāo)毀后臺(tái)的activity,導(dǎo)致activty的狀態(tài)丟失
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 the Back button, 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 about Managing Tasks.
因?yàn)閎ack stack中activity絕不會(huì)重新排列,如果你的應(yīng)用允許用戶(hù)從多于一個(gè)的activity打開(kāi)一個(gè)特別的activity,一個(gè)新的這個(gè)activity實(shí)例被建立并且壓入stack中(而不是把這個(gè)activity之前的任何實(shí)例帶到stack頂部)
就其本身而言,你應(yīng)用中的一個(gè)activity也許會(huì)被實(shí)例化多次(甚至從不同的task中),如上圖所示
同樣,如果用戶(hù)用back鍵導(dǎo)航回退,每一個(gè)activity實(shí)例會(huì)按著他們被打開(kāi)的順序依次顯露出來(lái)(帶著他們自己的UI狀態(tài))
不管怎樣,如果你不希望一個(gè)activity被實(shí)例化多次你可以修改這種行為。
如果修改將在下面的管理task章節(jié)討論
?
?
?
To summarize the default behavior for activities and tasks:
activity和task的默認(rèn)行為摘要
?
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 the Back button while in Activity B, Activity A resumes with its state restored.
When the user leaves a task by pressing the Home button, 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.
If the user presses the Back button, the current activity is popped from the stack and destroyed.?
The previous activity in the stack is resumed.?
When an activity is destroyed, the system does not retain the activity's state.
Activities can be instantiated multiple times, even from other tasks.
當(dāng)Activity A開(kāi)啟Activity B時(shí),Activity A進(jìn)入stop狀態(tài),但是系統(tǒng)保持Activity自己的狀態(tài)(比如滾動(dòng)的位置和文本輸入的形式)
在Activity B中如果用戶(hù)按下back鍵,Activity A會(huì)和它自己的狀態(tài)一起恢復(fù)。
當(dāng)用戶(hù)通過(guò)按Home鍵離開(kāi)一個(gè)task時(shí),當(dāng)前的activity處于stop并且它的task進(jìn)入后臺(tái)
系統(tǒng)保持task中每一個(gè)activity的狀態(tài)
如果之后用戶(hù)通過(guò)選擇最初打開(kāi)task的launcher圖標(biāo)打開(kāi)恢復(fù)這個(gè)task,task會(huì)進(jìn)入前臺(tái)并且恢復(fù)stack中最頂部的activity。
如果用戶(hù)按下back鍵,當(dāng)前的activity會(huì)被從stack中彈出并銷(xiāo)毀
上一個(gè)stack中的activity被恢復(fù)。
當(dāng)一個(gè)activity被銷(xiāo)毀后,系統(tǒng)不會(huì)保持這個(gè)activity的狀態(tài)
activity可以被實(shí)例化多次,甚至從其他的task中。
?
?
?
保存activity的狀態(tài)
?
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.
正如上面所討論的,當(dāng)activity進(jìn)入stop狀態(tài)時(shí),系統(tǒng)默認(rèn)行為保護(hù)activity的狀態(tài)。
這樣,當(dāng)用戶(hù)導(dǎo)航回退到上一個(gè)activity時(shí),它就會(huì)顯示離開(kāi)它時(shí)的UI
不管怎樣,你可以也應(yīng)該有意識(shí)的用回調(diào)函數(shù)保存你activity的狀態(tài) ,以防activity被銷(xiāo)毀并且必須被重新建立
?
?
?
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.
For more information about how to save your activity state, see the Activities document.
當(dāng)系統(tǒng)停止了你的一個(gè)activity時(shí)(例如一個(gè)新的activity打開(kāi)了或者task移動(dòng)到后臺(tái)),如果需要恢復(fù)系統(tǒng)內(nèi)存的話(huà),系統(tǒng)也許會(huì)完全銷(xiāo)毀這個(gè)activity。
當(dāng)發(fā)生這種情況,這個(gè)activity的有關(guān)狀態(tài)的信息將會(huì)丟失
如果發(fā)生這種情況,系統(tǒng)仍然知道這個(gè)activity在back stack占有一個(gè)位置,但是當(dāng)這個(gè)activity被帶到stack頂端時(shí),系統(tǒng)必須重新建立它(而不是復(fù)用它)。
為了避免丟失你的成果,你應(yīng)該有意識(shí)的通過(guò)在你的activity中實(shí)現(xiàn)onSaveInstanceState()這個(gè)回調(diào)函數(shù)來(lái)保持你的成果。
?
?
本文來(lái)自對(duì)下面地址的翻譯,英文水平實(shí)在有限,希望拍磚同時(shí)能給予指正。
http://developer.android.com/guide/components/tasks-and-back-stack.html
?
?
?
?
轉(zhuǎn)貼請(qǐng)保留以下鏈接
本人blog地址
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
