- 浏览: 580247 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (338)
- 已过时文章(留念用) (39)
- Android学习笔记 (30)
- Android开发指引自译 (100)
- Android NDK文档自译 (23)
- Android设计指引自译 (2)
- xp(ペケピー)&linux(理奈、铃)酱~ (4)
- ui酱&歌词自译~ (9)
- lua酱~ (9)
- 自我反省 (1)
- 羽game计划 (1)
- XSL酱 (2)
- java酱 (3)
- 设计的领悟 (58)
- 涂鸦作品(pixiv) (1)
- ruby酱 (2)
- Objective-C编程语言自译 (2)
- Android开发月报 (6)
- objc酱 (2)
- photoshop (3)
- js酱 (6)
- cpp酱 (8)
- antlr酱 (7)
- Lua 5.1参考手册自译 (11)
- 收藏品 (3)
- 待宵草计划 (4)
- 体验版截图 (1)
最新评论
-
naruto60:
太给力了!!!!我这网打不开Intel官网,多亏楼主贴了连接, ...
使用HAXM加速的Android x86模拟器(和一些问题) -
yangyile2011:
谢谢博主,翻译得很好哦
【翻译】(4)片段 -
ggwang:
牙痛的彼岸:痹!
牙痛的彼岸 -
ggwang:
总结得很简练清晰啊,学习了!
ANTLR学习笔记一:概念理解 -
leisurelife1990:
mk sdd
用git下载Android自带app的源代码
【翻译】(6)任务与后退堆栈
see
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
原文见
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
-------------------------------
Tasks and Back Stack
任务与后退堆栈
-------------------------------
Quickview
快速概览
* All activities belong to a task
* 所有活动属于一个任务
* A task contains a collection of activities in the order in which the user interacts with them
* 一个任务包含一组带有用户界面的有序活动
* Tasks can move to the background and retain the state of each activity in order for users to perform other tasks without losing their work
* 任务可以移到后台并保持每个活动的状态,以便让用户执行另一个任务而不会丢失他们的工作。
In this document
本文目录
* Saving Activity State 保存活动状态
* Managing Tasks 管理任务
* Defining launch modes 定义启动模式
* Handling affinities 处理关系
* Clearing the back stack 清除后退堆栈
* Starting a task 启动任务
Articles
文章
Multitasking the Android Way
Android的多任务手段
See also
另见
Android Design: Navigation
Android设计:导航
Application Lifecycle video
应用程序生命周期视频
<activity> manifest element
<activity>清单元素
-------------------------------
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.
应用程序通常包含多个活动。每个活动应该围绕用户可执行的一种特定类型动作进行设计,并能启动其它活动。例如,一个电子邮件应用程序可能拥有展示一列新电子邮件的活动。当用户选择一个电子邮件,会打开新的活动以便查看电子邮件。
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.
一个活动甚至可以启动存在于设备上的其它应用程序的活动。例如,如果你的应用程序想发送电子邮件,你可以定义一个意图去执行send动作并包含一些数据,诸如邮箱地址和消息。然后,打开声明它自己以处理此类意图的来自另一个应用程序的活动。这种情况下,意图是用来发送邮件,所以电子邮件应用程序的compose活动启动(如果多个活动支持同一个意图,那么系统让用户选择使用哪一个)。当电子邮件被发送时,你的活动恢复,并且好像电子邮件活动就是你的应用程序的一部分那样。尽管活动可以来自不同的应用程序,但Android通过在同一个任务中保持这两个活动以维护这种无缝的用户体验。
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.
一个任务是一组活动,用户在执行某些工作时用这些活动与用户交互。活动被安排成一个堆栈(后退堆栈),以每个活动被打开的顺序进行排列。
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.
设备(注:Android手机)的桌面屏幕放在大多数任务的起始位置。当用户触碰应用程序启动器中的一个图标(或桌面屏幕的快捷方式),应用程序的任务就会跑到前台。如果不存在该应用程序的任务(该应用程序最近没有被使用)。那么新的任务被创建并打开应用程序的main活动,作为堆栈的根活动。
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.
在当前活动启动另一个活动时,新活动被压进堆栈栈顶并获取焦点。前一个活动保留在堆栈中,但被停止。当一个活动停止,系统维护其用户界面的当前状态。当用户按下后退按钮,当前活动从堆栈栈顶弹出(活动被销毁)并恢复前一个活动(恢复它之前的用户界面状态)。堆栈中的活动永远不会被重新排列,只会在堆栈中压入和弹出——在当前活动启动时压进堆栈,在用户用后退按钮离开它时弹出堆栈。因此,后退堆栈的行为就像一个后进先出的对象结构。图1可视化了这一行为,用时间轴显示每个时间点上活动与当前后退堆栈之间的演变。
-------------------------------
(图略:
1. 前台活动:活动1
后退堆栈
1->2 启动活动2
2. 前台活动:活动2
活动1
2->3 启动活动3
3. 前台活动:活动3
活动2 活动1
3->5 导航后退
4. 前台活动:活动3 <- 活动3被销毁
5. 前台活动:活动2
活动1
)
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.
图1. 表示一个任务中每个新活动如何添加一个条目到后退堆栈中。当用户按下后退按钮时,当前活动被销毁并恢复前一个活动。
-------------------------------
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.
如果用户继续按后退键,那么堆栈中的活动逐一被弹出以显露前一个活动,直至用户返回到桌面屏幕(注:Home屏)(或返回到在任务开始时正在运行的那个活动)。当所有活动从堆栈中移除,堆栈将不再存在。
-------------------------------
(图略:
(左图)任务B:
前台活动:活动Z
活动Y
(右图)任务A:
后台:活动Y 活动X
)
Figure 2. Two tasks: Task B receives user interaction in the foreground, while Task A is in the background, waiting to be resumed.
图2. 两个任务:任务B接收前台中的用户交互,此时任务A正处于后台中,等待被恢复。
-------------------------------
(图略:
后退堆栈:
自下而上:桌面活动,活动2,桌面活动,活动1
单独的实例(指向两个Home活动)
)
Figure 3. A single activity is instantiated multiple times.
图3. 一个单一活动被多次实例化。
-------------------------------
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.
一个任务是一个粘合单元,可以在用户启动一个新堆栈或通过按HOME按钮跑到桌面屏幕时移到后台。在后台中,任务的所有活动被停止,但任务的后退堆栈维持完整——当另一个任务发生时这个任务简单地失去焦点,正如图2所示。之后任务可以返回到前台以使用户能重新获得它们离开的地方。例如,假设当前任务(任务A)在它的堆栈中有三个活动——两个在当前活动的下面。用户按下HOME按钮,然后从应用程序启动器中启动一个新应用程序。当桌面屏幕显示时,任务A跑到后台。当启动新的应用程序时,系统为应用程序启动一个任务(任务B),使用它自己的活动堆栈。在与应用程序交互之后,用户再次返回到桌面并选择原来启动任务A的应用程序。现在,任务A返回前台——在它的堆栈中所有三个活动是完好的,并且恢复堆栈栈顶的活动。此时,用户还可以通过跑到桌面选择启动那个任务的应用程序图标,以切换回任务B(或通过触碰并按住HOME按钮以显示最近任务并选择其中的一个)。这是Android多任务的一个示例。
-------------------------------
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.
注意:多个任务可以同时被后台持有。然而,如果用户正在同时运行很多后台任务,系统可能开始销毁后台活动以便回收内存,导致活动的状态被丢弃。参看下面关于活动状态的章节。
-------------------------------
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 popped 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.
因为后退堆栈中的活动从不被重新排列,所以如果你的应用程序允许用户从多于一个活动中启动一个特定活动,那个活动的新实例被创建并往上压进(注:弹出?,这里的意思可能是说,看上去好像向上移动了)堆栈(而非把活动之前的某个实例移到栈顶),正如图3所示。因此,你的应用程序的活动可能被实例化多次(甚至来自不同的任务),如图3所示。因此,如果用户使用后退按钮导航后退,那么活动的每个实例以它们曾经被打开的顺序展现(每个实例都有它们自己的用户界面状态)。然而,如果你不想让一个活动被实例化多次,你可以修改这一行为,在后面关于任务管理的章节中将讨论如何做到这一点。
To summarize the default behavior for activities and tasks:
总而言之,活动和任务的默认行为是:
* 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.
* 当活动A启动活动B时,活动A被停止,但系统保持它的状态(诸如滚动条位置和输入到表单中的文本)。如果用户在活动B时按下后退按钮,那么活动A恢复执行并恢复它原有状态。
* 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.
* 当用户通过按HOME按钮离开任务时,当前活动被停止并且它的任务转到后台。系统保持堆栈中每个活动的状态。如果之后用户通过选择曾经启动过某任务的启动器图标恢复任务时,任务跑到前台并恢复栈顶的活动。
* 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.
活动可以被多次实例化,甚至来自其它任务。
-------------------------------
Navigation Design
导航设计
For more about how app navigation works on Android, read Android Design's Navigation guide.
想获取更多关于Android上的应用导航是如何工作的信息,请阅读Android设计的导航指引。
-------------------------------
Saving Activity State
保存活动状态
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.
正如上面谈论的那样,系统的默认行为是活动停止的时候会保持活动的状态。这样,当用户导航后退到前一个活动时,它的用户界面表现他们离开的方式。然而,你可以——并且应该——主动使用回调方法保持你的活动的状态,如果活动被销毁并且必须被重新创建的话。
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.
当系统停止你其中一个活动时(诸如在新活动启动或任务移到后台时),如果它需要回收系统内存,那么系统可能完全地销毁那个活动。当发生这种情况时,活动的相关信息被丢失。如果发生这种情况,系统仍知道该活动在堆栈中占据一个位置,但当活动被带到栈顶时系统必须重新创建它(而非恢复它)。为了避免丢失用户的工作,你应该主动通过在你的活动中实现onSaveInstanceState()回调方法来保持它。
For more information about how to save your activity state, see the Activities document.
更多关于如何保存你的活动状态的信息,请参考活动文档。
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.
Android管理任务和后退堆栈的方式,正如上面所说——通过把启动成功的所有活动放置到同一个任务并放进一个后进先出的堆栈——对于大多数应用程序来说工作得很好,你不必担心你的活动如何与任务关联或它们如何存在于后退堆栈中。然而,你可以决定你希望打破这种通常的行为。可能你希望你的应用程序的一个活动在它开始时启动一个新任务(而非放进当前任务);或者,当你启动一个活动时,你希望把它现存的实例带到前面(而非在后退堆栈的顶部创建新实例);或者当用户离开任务时,你希望你的后退堆栈清空一切活动,除了根活动。
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()的意图中使用标志,做到这些事情甚至更多。
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:
而你可以使用的主要意图标志有:
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.
在以下章节中,你将看到你可以如何使用这些清单属性和意图标志去定义活动如何与任务关联以及在后退堆栈中如何行动。
-------------------------------
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 the BACK button. Be sure to test for navigation behaviors that might conflict with the user's expected behavior.
警告:大多数应用程序不应该打破活动和任务的默认行为。如果你确定你的应用必须修改默认行为,请小心使用并确保测试过,在启动期间,以及使用后退按钮从另一个活动和任务导航回到它时,活动的可用性。确保测试过可能与用户所期待的行为有冲突的导航行为。
-------------------------------
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:
启动模式允许你定义活动的新实例是如何关联到当前任务的。你可以用两种方式定义不同的启动模式:
* 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.
当你在你的清单文件中声明一个活动时,你可以指定活动在其启动时应该如何与任务关联。
* Using Intent flags
* 使用意图标志
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.
当你调用startActivity()时,你可以在意图中包含标志以声明新活动如何(或是否)与当前任务关联。
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).
因此,如果活动A启动活动B,活动B可以在它的清单中定义它如何与当前任务关联(如果有),而活动A还可以请求活动B应该如何与当前任务关联。如果两个活动都定义了活动B应该如何与任务关联,那么活动A的请求(在意图中定义)优于活动B的请求(在它的清单中定义)
-------------------------------
Note: Some the launch modes available in the manifest 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.
注意:清单中一些可用的启动模式作为意图的标志是不可用的,同样,一些作为意图的标志可用的启动模式不能定义在清单中。
-------------------------------
Using the manifest file
使用清单文件
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.
当在你的清单文件中声明一个活动时,你可以使用<activity>元素的launchMode属性指定活动应该如何与任务关联。
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属性指定一种关于活动应该如何启动成一个任务的指令。你可以为launchMode属性赋予四种不同的启动方式:
* "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.
默认值。系统创建以活动开始的任务中的活动新实例并把意图路由给它。活动可以被实例化多次,每个实例可以属于不同的任务,而一个任务可以拥有多个实例(注:多个活动实例)。
* "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).
如果一个活动实例总是存在于当前堆栈栈顶,那么系统通过调用它的onNewIntent()方法,把意图路由至那个实例,而非创建活动的新实例。活动可以被实例化多次,每个实例可以属于不同任务,而一个任务可以拥有多个实例(注:多个活动实例)(但仅当后退堆栈栈顶的活动不是活动的已存在实例时)。
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".
例如,假设一个任务的后退堆栈由根活动A、活动B、C和栈顶的D组成(堆栈为A-B-C-D;D在栈顶)。意图的目标是类型D的活动。如果D拥有默认的standard启动模式,那么该类的新实例被启动使堆栈变成A-B-C-D-D。然而,如果D的启动模式是singleTop,那么已存在的D实例通过onNewIntent()接收到意图,因为它在栈顶——堆栈仍然是A-B-C-D。然而,如果意图的目标是类型B的活动,那么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().
注意:当活动的一个新实例被创建时,用户可以按后退按钮返回前一个活动。但当活动的现存实例处理一个新意图时,用户不能按后退按钮返回到在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.
系统创建一个新任务并实例化新任务的根活动。然而,如果活动的实例已经存在于一个单独任务中,那么系统通过调用它的onNewIntent()方法,把意图路由至现存实例,而非创建一个新实例。同一时间只能存在一个活动实例。
-------------------------------
Note: Although the activity starts in a new task, the BACK button still returns the user to the previous 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.
和singleTask相同,但不同的是系统不会启动其它任意活动进去持有该实例的任务。活动总是单一的,并且只是它的任务的成员;任意由它启动的活动在独立的任务中打开。
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浏览器应用程序声明网页浏览器活动应该总是在它自己的任务中打开——通过在<activity>元素中指定singleTask启动模式。这意味着如果你的应用程序发送意图去打开Android浏览器,那么它的活动并不是放在和你的应用程序相同的任务中。取而代之,不管是启动新任务以获得浏览器,还是,如果浏览器已经有一个正在运行于后台的任务,那个任务都会被带到前面去处理新意图。
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.
不管是在新任务中,还是在与启动它的活动相同的堆栈中启动活动,后退按钮总是把用户带到前一个活动。然而,如果你启动一个活动,它指定singleTask启动模式,那么如果一个活动实例存在于一个后台任务中,整个任务就会被带到前台。在这一点上,后退堆栈现在包含来自被向前带的任务的所有活动,放在堆栈的顶部。图4描绘此类场景。
-------------------------------
(图略:
1. 后退堆栈 前台活动:活动2
后退堆栈:活动1
后台活动:活动Y (<- 用"singleTask"启动模式声明)活动X
1->2 启动活动Y
2. 后退堆栈 前台活动:活动Y
后退堆栈:活动X 活动2 活动1
2->3 导航后退
3. 后退堆栈 前台活动:活动X
后退堆栈:活动2 活动1
3->4 导航后退
3. 后退堆栈 前台活动:活动2
后退堆栈:活动1
)
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的活动如何被添加到后退堆栈。如果活动已经是拥有自己私有后退堆栈的后台任务的一部分,那么整个后退堆栈也会向前移,放到当前任务的上方。
-------------------------------
For more information about using launch modes in the manifest file, see the <activity> element documentation, where the launchMode attribute and the accepted values are discussed more.
更多关于在清单文件中使用启动模式的信息,请参见<activity>元素文档,那里会讨论更多关于launchMode属性和可接受值的问题。
-------------------------------
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.
注意:你用launchMode属性为你的活动指定的行为可以被启动活动的意图所包含的标志覆盖,正如下面章节讨论的那样。
-------------------------------
Using Intent flags
使用意图标志
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:
当启动一个活动时,你可以通过在传递给startActivity()的意图中包含标志,修改一个活动与它的任务的默认关系。你可以用来修改默认行为的标志有:
* 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().
在新任务中启动活动。如果一个任务已经为你现在启动的活动而运行,那么该任务被带到前台恢复它上一次的状态,并且活动在onNewIntent()中接收新意图。
This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.
它产生与singleTask的launchMode值相同的行为,在前面的章节中曾经讨论过。
* 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.
如果正在启动的活动是当前活动(在后退堆栈栈顶),那么现存实例接收对onNewIntent()的调用,而非创建活动的新实例。
This produces the same behavior as the "singleTop" launchMode value, discussed in the previous section.
它产生与singleTop的launchMode值相同的行为,在前面的章节中曾经讨论过。
* 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()).
如果正在被启动的活动总是在当前任务中运行,那么不是启动活动的新实例,而是销毁其顶部的其它所有活动,并通过onNewIntent()把这个意图传递给被恢复的活动实例(现在在栈顶)。
There is no value for the launchMode attribute that produces this behavior.
没有一种launchMode属性值可以产生这种行为。
FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with 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.
FLAG_ACTIVITY_CLEAR_TOP最常和FLAG_ACTIVITY_NEW_TASK一起组合使用。当一起使用时,这些标志是定位另一个任务内的现存活动并把它放到它能响应意图的地方的方式。
-------------------------------
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".
注意:如果特定活动的启动模式是standard,那么它还会从任务中移除并且在它的位置上启动新的实例以处理传入的意图。那是因为当启动模式是standard时,新实例总是为了新意图而被创建。
-------------------------------
Handling affinities
处理关系
The affinity indicates 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.
关系指示一个活动倾向于属于哪个任务。默认,相同应用程序的所有活动拥有同一个关系。所以默认下,相同应用程序中的所有活动倾向于放在相同的任务中。然而,你可以修改一个活动的默认关系。定义在不同应用程序中的活动可以共享一个关系,或者定义在同一个应用程序中的活动可以被赋予不同的任务关系。
You can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.
你可以使用<activity>元素的taskAffinity属性修改任意给定活动的关系。
The taskAffinity attribute takes a string value, which must be unique from the default package name declared in the <manifest> element, because the system uses that name to identify the default task affinity for the application.
taskAffinity属性携带一个字符串值,它必须不同于在<manifest>元素中声明的默认包名,因为系统使用那个名称为应用程序标识默认的任务关系。
The affinity comes into play in two circumstances:
关系起作用有两种情况:
* When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.
* 当启动活动的意图包含FLAG_ACTIVITY_NEW_TASK标志。
A new activity is, by default, launched into the task of the activity that called startActivity(). It's pushed onto the same back stack as the caller. However, if the intent passed to startActivity() contains the FLAG_ACTIVITY_NEW_TASK flag, 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.
默认,新活动在调用startActivity()的活动的同一个任务中启动。它作为调用方被压进相同的后退堆栈。然而,如果传递给startActivity()的意图包含FLAG_ACTIVITY_NEW_TASK标志,那么系统寻找不同的任务去容纳新的活动。通常,是一个新任务。然而,并非一定是这样。如果已经有一个现存任务,和新活动具有相同的关系,那么活动在那个任务中启动。如果没有,它才会启动新任务。
If this flag causes an activity to begin a new task and the user presses the HOME button 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 put FLAG_ACTIVITY_NEW_TASK in the intents they pass to startActivity(). 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 a CATEGORY_LAUNCHER intent filter; see the Starting a task section below).
如果这个标志导致一个活动启动新任务,而用户按HOME按钮离开,那么必须有一些方法让用户导航回到任务。一些实体(诸如通知管理器)总是在外部任务中启动活动,从不作为它们自身的一部分,所以它们总是放置FLAG_ACTIVITY_NEW_TASK在传递给startActivity()的意图中。如果你有一个可以被外部实体调用的活动可能使用了这个标志,请注意用户有一种独立的方法返回它启动的任务,诸如用一个启动器图标(任务的根活动有CATEGORY_LAUNCHER意图过滤器;参见下面启动任务的章节)。
* When an activity has its allowTaskReparenting attribute set to "true".
* 当活动的allowTaskReparenting属性被设置为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.
例如,假设一个报道选中城市的天气状况的活动被定义为一个旅游应用程序的一部分。它与同一个应用程序中其它活动拥有相同的关系(默认的应用程序关系)并且允许用这个属性重新定义父级(注:父级任务)。当你的其中一个活动启动天气报道器的活动时,它最初属于和你的活动相同的堆栈。然而,当旅游应用程序的堆栈跑到前台时,天气报道器活动会被重新赋予到那个任务并在它里面显示。
-------------------------------
Tip: If an .apk file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity attribute to assign different affinities to the activities associated with each "application".
提示:如果一个.apk文件从用户的视角看包含多于一个“应用程序”,你可能希望使用taskAffinity属性,赋予不同关系给与每个“应用程序”关联的活动。
-------------------------------
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.
如果用户长时间离开一个任务,系统清除任务中的所有活动,除了根活动。当用户再次返回到任务,只有根活动被恢复。系统以这种方式行动,是因为在被延长的较长时间后,用户可能已经放弃他们之前正在做的事情并返回到那个任务去做新的事情。
There are some activity attributes that you can use to modify this behavior:
你可以使用一些活动属性去更改这种行为:
* 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.
如果在任务的根活动中这个属性被设置为true,上面描述的默认行为不会发生。任务保持堆栈中的所有活动,即便在长时间后。
* 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 of alwaysRetainTaskState. The user always returns to the task in its initial state, even after a leaving the task for only a moment.
如果在任务的根活动中这个属性被设置为true,那么不管用户何时离开任务并返回到它,任务都会向下清除直至根活动。换言之,它与alwaysRetainTaskState相反。用户总是以任务的初始状态返回到它,即便仅仅离开任务一阵子。
* finishOnTaskLaunch
This attribute is like clearTaskOnLaunch, 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,但它操作在单一活动上,而非整个任务。它也可能导致任意活动离开,包括根活动在内。当它被设置为true时,活动仅为当前会话保持活动中的部分。如果用户离开然后又返回到任务,那么它将不再存在。
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作为指定动作并且用android.intent.category.LAUNCHER作为指定分类。例如:
-------------------------------
<activity ... >
<intent-filter ... >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
...
</activity>
-------------------------------
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.
此类意图过滤器导致活动的图标和标签被显示在应用程序的启动器中,给予用户一种启动活动和返回到任务的方式,在它被启动之后能随时创建它。
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 two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. 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 the HOME button. 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.
第二个功能十分重要:用户必须有能力离开任务然后在之后使用其活动启动器来返回到它。因此,两种被标记为singleTask和singleInstance的活动的启动模式,总是初始化一个任务,仅当活动拥有ACTION_MAIN和CATEGORY_LAUNCHER过滤器。请设想一下,例如,如果缺少过滤器的话可能发生的情况:一个意图启动一个singleTask的活动,初始化一个新任务,然后用户花一些时间在任务中工作。然后用户按下HOME按钮。任务现在被发送到后台并且不再可见。现在用户无法返回到任务,因为它并没有被呈现在应用程序启动器中。
For those cases where you don't want the user to be able to return to an activity, set the <activity> element's finishOnTaskLaunch to "true" (see Clearing the stack).
对于那些你不愿意让用户能返回一个活动的情况,请设置<activity>元素的finishOnTaskLaunch属性为true(参看清除堆栈)。
Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.
除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。
Android 4.0 r1 - 27 Oct 2011 21:16
Android 4.0 r1 - 20 Jan 2012 22:25
-------------------------------
patch:
1. Android 4.0 r1 - 20 Jan 2012 22:25
(1)
Back key
->
Back button
(2)
Figure 2. Two tasks: Task A is in the background, waiting to be resumed, while Task B receives user interaction in the foreground.
->
Figure 2. Two tasks: Task B receives user interaction in the foreground, while Task A is in the background, waiting to be resumed.
(3)
Home key
->
Home button
(4)
++
Navigation Design
For more about how app navigation works on Android, read Android Design's Navigation guide.
(5)
Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the BACK key always takes the user to the previous activity. However,
if you start an activity from your task (Task A) that specifies the singleTask launch mode, then that activity might have an instance in the background that belongs to a task with its own back stack (Task B). In this case, when Task B is brought forward to handle a new intent, the BACK key first navigates backward through the activities in Task B before returning to the top-most activity in Task A. Figure 4 visualizes this type of scenario.
->
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.
(6)
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 (Task B), then the entire back stack also comes forward, on top of the current task (Task A).
->
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.
(7)
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 two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. 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 the HOME key.
The task is now sent to the background and not visible. Because it is not represented in the application launcher, the user has no way to return to the 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 two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. 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 the HOME button.
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.
(8)
图基本上都被修改过
-------------------------------
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)
发表评论
-
【翻译】(9-补丁2)电话簿提供者
2012-07-18 12:54 2400【翻译】(9-补丁2)电话簿提供者 see h ... -
【翻译】(8-补丁1)Android接口定义语言(AIDL)
2012-07-02 05:55 2927【翻译】(8-补丁1)Andro ... -
【翻译】(0)应用组件
2012-06-30 23:50 829【翻译】(0)应用组件 see http:// ... -
【翻译】(88)传感器
2012-05-21 21:25 1077【翻译】(88)传感器 ... -
【翻译】(87)复制与粘贴
2012-05-20 14:48 1929【翻译】(87)复制与粘贴 see http: ... -
【翻译】(86)音频捕捉
2012-05-16 15:14 1104【翻译】(86)音频捕捉 ... -
【翻译】(85)照相机
2012-05-13 15:09 3796【翻译】(85)照相机 see http:// ... -
【翻译】(84)JetPlayer
2012-04-21 16:24 981【翻译】(84)JetPlayer see h ... -
【翻译】(83)媒体回放
2012-04-21 16:00 1860【翻译】(83)媒体回放 see http:/ ... -
【翻译】(82)多媒体与照相机
2012-04-18 23:05 953【翻译】(82)多媒体与照相机 see htt ... -
【翻译】(23-补丁3)构建无障碍服务
2012-04-18 21:57 1627【翻译】(23-补丁3)构 ... -
【翻译】(23-补丁2)使应用程序无障碍
2012-04-16 13:08 2099【翻译】(23-补丁2)使应用程序无障碍 see ... -
【翻译】(23-补丁1)无障碍
2012-04-11 22:38 911【翻译】(23-补丁1)无 ... -
【翻译】(81)Renderscript之运行时API参考手册
2012-04-11 22:13 1416【翻译】(81)Renderscript之运行时API参 ... -
【翻译】(80)Renderscript之计算
2012-04-09 14:09 1444【翻译】(80)Renderscript之计算 ... -
【翻译】(79)Renderscript之图形
2012-04-08 13:59 2866【翻译】(79)Renderscript之图形 ... -
【翻译】(78)Renderscript
2012-04-04 15:35 1442【翻译】(78)Renderscript see ... -
【翻译】(77)可绘画对象动画
2012-03-18 10:52 711【翻译】(77)可绘画对象动画 see htt ... -
【翻译】(76)视图动画
2012-03-18 10:04 806【翻译】(76)视图动画 see http:/ ... -
【翻译】(75)属性动画
2012-03-17 18:24 2508【翻译】(75)属性动画 see http:/ ...
相关推荐
uCOS 任务堆栈的大小是可以根据实际需要进行调整的,它的大小与任务代码中临时变量的数目有关。如果这段代码临变量特别多,堆栈就应设计的大一些。但是,uCOS 任务堆栈的初始化仅仅是初始化了很大一个堆栈的一小部分...
**Android-Okuki详解:构建分层导航与后退堆栈** Okuki 是一个专为 Android 应用设计的轻量级框架,旨在简化应用程序中的分层导航和管理后退堆栈。它提供了灵活的解决方案,使开发者可以更加优雅地处理复杂的导航...
6. 分析结果,根据需要调整任务资源分配。 7. 运行系统并观察性能变化。 总结来说,FreeRTOS通过其丰富的API提供了强大的任务管理和系统监控能力。通过深入理解和熟练运用这些API,开发者可以有效地调试STM32F103C8...
删除任务会释放与之相关的所有资源,包括堆栈空间。请注意,删除任务时需要确保不会破坏系统的其他部分,比如删除一个还在执行的任务可能会导致系统异常。 在ESP32中,FreeRTOS被集成到ESP-IDF(Espressif IoT ...
### ucos 任务堆栈详解 #### 一、引言 在实时操作系统(RTOS)的领域里,uC/OS-II以其高效稳定而著称,广泛应用于嵌入式系统开发中。其中,任务堆栈管理是uC/OS-II的核心概念之一,对于确保任务正确调度和执行至关...
使用 Windows Phone 的后退堆栈导航 示例 [ PhoneAppBackStack ] http://blog.csdn.net/kinleyhao/article/details/8891630
在实际应用中,LabVIEW的堆栈状态机可以用于各种场景,比如处理序列操作、实现复杂的控制逻辑,或者在多任务环境中管理任务的执行顺序。通过巧妙地使用堆栈,开发者可以创建出高效且易于理解的程序结构。理解并熟练...
android 任务跟回退堆栈
在多任务环境中,每个任务都有自己的堆栈空间用于存储局部变量和函数调用信息。堆栈溢出可能导致系统崩溃或不稳定,因此定期进行堆栈检查至关重要。在实验中,我们将学习如何使用工具或自定义函数来监测堆栈使用情况...
通过分析这些变化,可以了解程序如何与内核交互,以及堆栈如何处理内核函数调用和中断处理。 在Linux 0.11中,堆栈通常在物理内存的高端分配,这是因为内核必须能够快速访问它,同时避免与用户空间的内存区域冲突。...
可以设置一个堆栈边界标志,并在运行时比较堆栈指针与该标志的位置。如果发现指针超过了边界,就表明发生了溢出。 3. **编译器提供的工具**:像Keil MDK这样的编译器套件提供了堆栈分析工具,可以在编译时或运行时...
标题中提到的"C51堆栈构成与空间需求分析",指向的是对51系列单片机中堆栈的结构及其所需空间的详细分析。51系列单片机(包括其衍生型号)是8位微控制器,广泛应用于嵌入式系统开发中。堆栈空间是其中非常重要的一...
描述中提到的“堆栈动作机”可能是一个特定的程序或者模块,它使用LabVIEW来实现堆栈的操作,并可能包含了其他与之相关的功能,如错误处理、数据验证等。动作机(Action Machine)可能是指一种自动化流程,用于执行...
启动服务、启动线程、得到任务堆栈列表的简单代码 代码在模拟机、真机上都测试通过。android 1.6的平台。 都是很简单的代码,不复杂。特别是得到任务堆栈列表,以前网上看到的代码功能都比较多,比较复杂,作为菜鸟...
在`william_test_muti`中,变量var与z相乘的结果被存储回[r11, -0x14]。 5. **函数调用**: 当函数调用其他函数时,如`taskSuspend(0)`,调用的函数地址(_taskSuspend)会通过BL指令压入堆栈,并将参数(在这里是...
6. 获取堆栈大小(Size):返回堆栈中元素的数量。 7. 清空堆栈(Clear):删除所有元素,将堆栈恢复为空状态。 在本项目中,为了实现对不同类型数据的支持,采用了泛型编程。泛型编程允许我们编写不依赖特定数据...
在Delphi这样的面向对象的编程语言中,理解和掌握队列(Queue)与堆栈(Stack)的使用对于编写高效、逻辑清晰的代码至关重要。 **队列(Queue)**: 队列是一种先进先出(First In First Out,简称FIFO)的数据结构...
在IT领域,尤其是在数字图像处理和多媒体应用中,图片堆栈和视频处理是常见的操作。MATLAB作为一种强大的数值计算和编程环境,提供了丰富的工具箱,包括图像处理工具箱,使得处理图片堆栈和制作视频变得非常高效。...
在FreeRTOS的任务创建时,为每个任务提供一个标识符,这样在显示堆栈使用情况时,可以清楚地知道哪个任务正在使用多少堆栈空间。例如: ```c TaskHandle_t xTaskHandle1; void vTask1(void *pvParameters) { // ...