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

android task与back stack 开发文档翻译 - 1


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.
一个应用通常包含多个activity
每个activity都应该被设计成围绕着一种用户可执行并且能打开其他activity明确的action
例如:一个邮件应用也许有一个activity用来显示新邮件列表
当用户选中一封邮件时,一个新activity被打开,用来查看这封邮件。


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.
一个activity甚至可以打开设备中存在于其他应用的activity
例如:如果你的应用想发送一封邮件,你可以定义一个intent用来执行一个发送的action并且包含一些数据,比如邮件地址和信息
其他应用中,声明它自己可以处理这样的intent的activity会打开。
这个例子中,intent用来发送邮件,所以一个邮件程序的"compose" activity会启动(如果多个activity都支持这个intent,那么系统会让用户选择使用哪一个)
当邮件发送完毕,你的activity会resume并且邮件的activity看上去好像你的应用的一部分
虽然activity可能来自不同的应用,Android通过保持两个应用的activity在同一个task中来保持这种无缝的用户体验。


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.
一个task是用户执行一个任务时,与用户交互的activity的一个集合
这些activity按着被打开的顺序被整理到一个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.
设备的Home页面是大多数task的起始位置。
当用户触摸一个在launcher应用中的图标(或者Home页面中的快捷方式)时,应用的task就会来到前台。
如果这个应用还没有task(这个应用最近没有被使用过),那么一个新的task会被建立并且应用的“主”activity作为stack的根activity被打开。


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 inte易做图ce.
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.
当当前activity打开另一个activity时,新的activity会被推入stack的顶部并且获得焦点
前一个activity仍然在stack中,但是处于stopped状态
当一个activity处于stop状态时,系统会保持它当前的UI状态
当用户按back键时,当前的activity从stack顶部弹出(activity会被销毁)并且上一个activity变成resume状态(上一个UI状态被恢复)
stack中的activity绝不会重新排列,在stack中只有压入和弹出 —— 当被当前的activity启动时压入stack,当用户使用back键离开它时弹出。
就其本身而言,back stack操作是像“后进先出”的对象结构一样
下图通过程序在多个activity之间连同当前的back stack适时地在每一个点的时间线展示了这种行为。

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中每一个新activity如何向添加一个条目到back stack的展示。
当用户按下back键时,当前的activity被销毁并且恢复上一个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.
如果用户继续按back键,那么每一个在stack中的activity都会弹出暴露出上一个activity,直到用户返回到Home(或者退回到task开始时运行的activity)
当所有activity都被从stack中移除之后,task也就不复存在了。


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是一个紧密结合的单元,当用户打开一个新的task或者浏览Home主屏时,task可以移动到“后台”
当在后台时,task中所有的activity都是stopped状态,但是task的back stack保持完整,当另一个task占据位置时之前的task只是简单的失去焦点,如上图所示。
task可以返回到前台,所以用户可以回到他们离开的地方。
假设:例如当前的task(task A)在他的stack中有3个activity,其中两个在当前activity下面。
用户按下Home键,然后从launcher应用启动一个新的应用。
当home页面出现的时候,task进入后台。
当新的应用开启时,系统为这个新应用用它自己activity的stack开启一个task(Task B)
与新应用交互完成之后,用户返回Home然后选择启动task A的原始应用。
现在,Task A来到前台,stack中的3个activity原封不动没有改变,stack最顶部的activity重新开始。
这时候,用户可以通过去Home选择启动Task B的应用图标转换回到Task

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,