Android中Task、Activity、BackStack(栈)的关系

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html



                                                


An application usually containsmultiple activities.Each activity should be designed around a specific kind of action the user canperform and can start other activities. For example, an email application mighthave 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都应该围绕着一种特定的action来设计,用户可以执行这个action,并能够启动其他的activity。例如,一个email程序可能有一个activity来显示新邮件的列表。当用户选择一个邮件,启动一个新的activity来浏览它。

An activity can even start activities thatexist in other applications on the device. For example, if your applicationwants 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. Anactivity from another application that declares itself to handle this kind ofintent then opens. In this case, the intent is to send an email, so an emailapplication's "compose" activity starts (if multiple activitiessupport the same intent, then the system lets the user select which one touse). When the email is sent, your activity resumes and it seems as if theemail activity was part of your application. Even though the activities may befrom different applications, Android maintains this seamlessuserexperience by keeping both activities in the same task.

一个Activity甚至可以启动其他应用程序中的Activity。例如,如果你的应用程序想要发送一封email,你可以定义一个intent来执行一个“发送”的action,action中含有你要发送的数据,例如地址和信息。另一个应用程序中的Activity申明它能够处理这个intent。在这个例子中,intent是意图是发送email,于是就启动了一个发送email的程序(如果有多个activity支持,这样一个intent,那么系统会让用户来选择)。当一个email发送完成,你的activity恢复,就好像email的activity是你程序的一部分。虽然多个activity可能来自于不同的应用程序,android通过在一个task中使用多个activity来保证用户的无缝体验。

A task is a collection of activities thatusers interact with when performing a certain job. The activities are arrangedin a stack (the"back stack"), in the orderin which each activity is opened.

一个task是一个activity的集合,用户在执行某项任务时,和这些activity进行交互。这些activity按照他们被启动的顺序放在一个栈("backstack")中。

The device Home screen is the startingplace 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 theforeground. If no task exists for the application (the application has not beenused recently), then a new task is created and the "main" activityfor that application opens as the root activity in the stack.

设备的Home界面是大部分task启动的位置。当一个用户触摸了应用程序的图标,那么这个程序的task就会启动。如果这个程序在android系统中是没有被启动,那么一个新的task就被创建了,应用程序的“main”activity就会作为根activity,放入堆栈。

When the current activity starts another,the new activity is pushed on the top of the stack and takes focus. Theprevious activity remains in the stack, but is stopped. When an activity stops,the system retains the current state of its user interface. When the userpresses the Back button, the current activity is popped from the topof the stack (the activity is destroyed) and the previous activity resumes (theprevious 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 thecurrent activity and popped off when the user leaves it usingthe Back button. As such, the back stack operates as a "last in,first out" object structure. Figure 1 visualizes this behavior with atimeline showing the progress between activities along with the current backstack at each point in time.

当当前的activity启动了另一个activity时,新的activity会被放在(push)栈顶并得到焦点。之前的activity仍然在栈中,只不过它停止了。当一个activity停止,系统会保持当前用户界面的状态。当用户按下返回键,当前的activity会从栈顶弹出(此activity就销毁了(destroyed)),而它之前的那个activity会恢复。当某个activity压入到栈中或者从栈顶弹出,该activity将不会被处理。

If the user continues to press Back,then each activity in the stack is popped off to reveal the previous one, untilthe user returns to the Home screen (or to whichever activity was running whenthe task began). When all activities are removed from the stack, the task nolonger exists.

如果用户不断地按返回键,则每一个activity都会从栈中弹出,并显示它之前的那个activity,直到返回到Home主屏幕,或者是task启动时正在运行的一个activity。让所有的activity都从栈中移除,则task不在存在。

A task is a cohesive unit that can move tothe "background" when users begin a new task or go to the Homescreen, via the Home button. While in the background, all the activitiesin the task are stopped, but the back stack for the task remains intact—thetask has simply lost focus while another task takes place, as shown in figure2. A task can then return to the "foreground" so users can pick upwhere theyleft off. Suppose, for example, that thecurrent task (Task A) has three activities in its stack—two under the currentactivity. The user presses the Home button, then starts a newapplication from the application launcher. When the Home screen appears, Task Agoes into the background. When the new application starts, the system starts atask for that application (Task B) with its own stack of activities. Afterinteracting with that application, the user returns Home again and selects theapplication that originally started Task A. Now, Task A comes to theforeground—all three activities in its stack are intact and the activity at thetop of the stack resumes. At this point, the user can also switch back to TaskB by going Home and selecting the application icon that started that task (orby touching and holding the Home button to reveal recent tasks andselecting one). This is an example of multitasking on Android.

Task是一个紧密的单元,它能够被移动到后台,当用户启动一个新的task或者进入home主屏幕。当在后台时,task中所有的activity都将停止,但是这个栈仍然是完整的-这个task只是失去了焦点,被另一个task占据。用户可以将task从他们放入后台的位置,重新放到前台。例如,当前的task有三个activity,两个activity在当前activity的后卖弄。当用户按下Home键,然后启动里一个应用程序。当Home主屏幕显示,Task A就进入后台。当新的应用程序启动,系统为该应用程序启动一个新的task,并带有task它自己的栈。当用户完成对这个新的应用程序的使用,用户返回Home,启动之前的那个应用程序。于是Task A返回前台,三个activity都完整保存在栈中,并在屏幕上显示栈顶的那个activity。


Because the activities in the back stackare never rearranged, if your application allows users to start a particularactivity from more than one activity, a new instance of that activity iscreated and pushed onto the stack (rather than bringing any previous instanceof the activity to the top). As such, one activity in your application might beinstantiated multiple times (even from different tasks), as shown in figure 3.As such, if the user navigates backward using the Back button, eachinstance of the activity is revealed in the order they were opened (each withtheir own UI state). However, you can modify this behavior if you do not wantan activity to be instantiated more than once. How to do so is discussed in thelater section about ManagingTasks.

由于保存在栈中的activity是不可操作的,如果你的应用程序允许用户从不止一个activity中去启动同一个指定的activity,那么它每次都会创建一个这个指定activity的实例,并放入栈中(而不是将之前的栈中的activity放置到栈顶)。你可以修改这种行为,如果你不希望一个activity被创建多个实例,查看 ManagingTasks.。

to summarize the default behavior foractivities and tasks:

When Activity A starts Activity B, ActivityA is stopped, but the system retains its state (such as scroll position andtext entered into forms). If the user presses the Back button whilein Activity B, Activity A resumes with its state restored.

当Activity A启动Activity B,Activity A停止,但是系统会保持Activity A的状态,例如滚动条的位置,是form中输入的文字。当用户按下返回键,可以从Activity B返回到Activity A

When the user leaves a task by pressingthe Home button, the current activity is stopped and its task goesinto the background. The system retains the state of every activity in thetask. If the user later resumes the task by selecting the launcher icon thatbegan the task, the task comes to the foreground and resumes the activity atthe top of the stack.

当用户按下Home键,当前Activity停止,它的task进入后台。系统保持task中所有的activity的状态。如果用户通过重新启动改程序返回,这个task会重返前台,并恢复显示其栈顶的activity。

If the user pressesthe Back button, the current activity is popped from the stack anddestroyed. The previous activity in the stack is resumed. When an activity isdestroyed, the system does not retain the activity's state.

如果用户按下返回键,当前activity会从栈中弹出,并销毁。前一个activity从栈中恢复。当一个activity销毁,系统不会保持它的状态。

Activities can be instantiated multipletimes, even from other tasks.

一个activity可以被实例化多次,甚至是来自其他的task。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值