android task與back stack 開發文檔翻譯 – 2

Managing Tasks
管理task
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.
android管理task和back stack的方式,像上面描述的那樣-通過在相同的task中連續的放置打開的activity並且用一個“後進先出”的stack,為大多數應用很好的工作,你沒必要擔心如何將你的activity與task關聯或者他們如何存在於back stack中。
也許你想要你應用中的一個activity被打開時開始一個新的task(代替放在當前的task中);或者當你打開一個activity時,你想把之前已經存在的此activity實例帶到前臺(代替在back stack頂部新建一個實例);或者當用戶離開這個task時,除瞭根activity,你想要清空stack中的所有activity。

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().
使用在<activity>中的清單元素的屬性和傳遞給startActivity()函數的intent中的標志,你可以做到上面那些並且可以做的更多

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:
你可以使用的主要的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.
下面的章節中,你會看到如何使用這些清單屬性和intent標志來定義如何把activity與task關聯和back stack如何表現。

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:
啟動模式允許你定義如何實例化一個與當前task關聯的activity
你可以通過兩種方式定義不同的啟動模式

Using the manifest file
1使用manifest文件
When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts.
當你在你的manifest文件中定義一個activity時,你可以指定當這個activity打開時,它應該如何與task關聯。

Using Intent flags
2使用intent標志
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.
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).
當你調用startActivity()時,你可以在intent中包含一個標志來聲明:新activity應該如何(或者是否)與當前的task關聯
如上所述,如果Activity A開啟Activity B,Activity B可以在它的manifest中定義應當如何與當前的task關聯,Activity A也可以請求Activity B應該如何與當前的task關聯
如果兩個activity都定義瞭Activity B應該如何與一個task關聯,那麼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
使用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,你可用使用<activity>元素的launchMode屬性指定這個activity應該如何與task關聯

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屬性指定瞭一個此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.
standard(默認模式)
系統在task從開啟它的activity建立瞭一個新的activity然後按路線發送intent到它。
activity可以被實例化多次,每一個實例可以屬於不同的task,一個task可以有多個相同的activity實例。

"singleTop"
    If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
    The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the the activity at the top of the back stack is not an existing instance of the activity).
如果一個activity實例已經存在於當前task的頂部,系統會通過調用這個實例的onNewIntent()方法將intent送達到這個實例,而不是新建一個此activity實例。
這個activity可以被實例化多次,每個實例可以屬於不同的task,並且一個task可以擁有多個此activity實例(但是必須是在這個back stack頂部的activity並不是前面所述的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 through onNewIntent(), because it's at the top of the stack—the stack remains A-B-C-D.
    However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is "singleTop".
例如:假設一個task的back stack由A、B、C、D四個activity組成,其中A為根activity,順序為A、B、C、D
一個請求類型D的intent到達。
如果D使用的是標準啟動模式,一個新的D實例會被啟動,stack變為A-B-C-D-D。
然而,如果D的啟動模式為singleTop,那麼已經存在的D的實例通過onNewIntent()收到這個intent,這是因為它在stack的頂端,stack保持著A-B-C-D的順序
如果一個請求類型B的activity的intent發出,那麼一個B的新實例會被添加到stack,即使B的啟動模式為singleTop也一樣。

    Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity.
    But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived in onNewIntent().
註意:當一個activity的時候被建立,用戶可以按back鍵返回到上一個activity。
但是當一個已經存在的activity處理一個新intent時,用戶不可能按back鍵回到intent到達activity的onNewIntent()之前的狀態。

"singleTask"
    The system creates a new task and instantiates the activity at the root of the new task.
    However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.
    Only one instance of the activity can exist at a time.
系統建一個一個新的task並且實例化這個activity作為task的根。
然而,一個activity的實例已經存在於一個不同的task中,系統會通過調用已經存在的實例的onNewIntent()方法將intent送達到這個實例,而不是新建一個此activity實例。
隻有一個activity的實例可以同時存在

    Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.
雖然這個activity開啟瞭一個新的task,但是back鍵仍然會是用戶返回到上一個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的唯一成員;任何被他打開dactivity都會在不同的task中。

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.
This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application.
Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.
另一個例子中,Android瀏覽器應用聲明web瀏覽器aicivity應該在<activity>元素中通過指定singleTask啟動模式,打開到其自己的task中。
這意味著:如果你的應用發出一個intent來打開Android瀏覽器,瀏覽器的activity不會作為你的應用被放置到相同的task中。

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

不論是否activity開啟在一個新task中,或者和開啟它的activity在相同的task中,back鍵總是把用戶帶到上一個activity。
如果你打開一個指定瞭singleTask啟動模式的activity,那麼如果一個此activity的實例存在於後臺task中,整個task都會被帶到前臺。
這個時候,back stack包含瞭被帶到前臺的task中所有的activity,放置到stack的頂部。
下圖闡明瞭這種情況。

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.
一個啟動模式為singleTask的activity如何添加到back stack的陳述。
如果這個activity已經是後臺task的一部分,那麼他的整個back stack也會來到前臺,在當前task的頂部。

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.
註意:你為你的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 to startActivity().
The flags you can use to modify the default behavior are:
當開啟一個activity,你可以通過在傳給startActivity()函數中的intent中包含flags來修改activity與它的task的默認關聯
你可以使用flags來修改的默認行為有:

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().
在新的task中打開一個activity
如果你正在打開的activity已經運行在一個task中,那個task會被帶到前臺恢復到它最近的狀態並且這個activity會在onNewIntent()收到這個新的intent

    This produces the same behavior as the "singleTask" launchMode value, 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 to onNewIntent(), instead of creating a new instance of the activity.
如果這個activity被打開在當前activity(在back stack頂部),那麼已經存在的實例的onNewIntent()會被調用,代替新建一個此activity的實例。

    This produces the same behavior as the "singleTop" launchMode value, 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), through onNewIntent()).
如果要打開的activity已經運行在當前task中,那麼不再啟動此activty一個新實例,取而代之,所有在此activity上的其他 activity會被銷毀,並且這個intent通過onNewIntent()會發送到恢復的此activity中(現在在最task上面)

    There is no value for the launchMode attribute that produces this behavior.
沒有啟動模式屬性會導致此行為

    FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK.
FLAG_ACTIVITY_CLEAR_TOP最常用的用法是與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.
一起使用時,這些標識是定位一個已經在另一個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,它也會被從stack中移除,並且一個新的實例被啟動來處理到來的intent
那是因為如果啟動模式是standard,一個新的intent總是會創建一個新的實例。

 

作者:su1216

發佈留言