展会信息港展会大全

android task与back stack 开发文档翻译 - 2
来源:互联网   发布日期:2016-01-14 12:43:34   浏览:1668次  

导读: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 sho...

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

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港