当前位置:操作系统 > 安卓/Android >>

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.
补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,