任务和返回栈

Tasks and Back Stack

任务和返回栈

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. For example, an email app might have one activity to show a list of new messages. When the user selects a message, a new activity opens to view that message. This new activity is added to the back stack. If the user presses the Back button, that new activity is finished and popped off the stack. The following video provides a good overview of how the back stack works.

任务是执行某一确定任务时与用户交互的Activity集合。按照每个Activity打开的顺序,这些Activity被排列到一个堆栈----返回栈中。例如,一个邮件app可能有一个显示新消息列表的Activity。当用户选择一条消息,打开一个新的Activity去显示该消息。这个新的Activity会被添加到返回栈。如果用户按下返回按钮,这个Activity会结束运行并从返回栈弹出。下面的视频提供了一个关于堆栈如何工作的良好概述。

When apps are running simultaneously in a multi-windowed environment, supported in Android 7.0 (API level 24) and higher, the system manages tasks separately for each window; each window may have multiple tasks. The same holds true for Android apps running on Chromebooks: the system manages tasks, or groups of tasks, on a per-window basis.

当多个app同时运行在多窗口环境中,在Android 7.0和更高级别下,系统分别为每个窗口管理任务;每个窗口可能有多个任务。这同样适用于Android app运行在Chromebooks上:系统在每个窗口基础上管理任务或一组任务。

The device Home screen is the starting place for most tasks. When the user touches an icon in the app launcher (or a shortcut on the Home screen), that app's task comes to the foreground. If no task exists for the app (the app has not been used recently), then a new task is created and the "main" activity for that app opens as the root activity in the stack.

设备主屏幕是大多任务开始的地方。当用户触摸应用程序启动器图标(或者主屏幕的快捷方式)时,app的任务会出现在前台。如果app没有任务存在(app近期没有被使用过),然后创建一个新任务,并将该应用程序的“主”活动作为堆栈中的根活动打开。

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.

当当前的Activity启动另一个Activity时,新的Activity被压入栈顶并获取焦点。原来的Activity仍然保留在栈里面,但是是停止状态。当Activity停止时,系统保留了用户界面的当前状态。当用户按下返回按钮时,当前Activity从堆栈顶部弹出(Activity被销毁)并且原来的Activity重新恢复(恢复它之前的UI状态)。堆栈中的Activity永远不会重新排列,仅仅只会被从栈里压入或者弹出——当从当前Activity启动的时候入栈并且当用户使用返回按钮离开的时候出栈。像这样的,返回栈操作类似“后进先出”的对象结构。图1用一个时间线可视化了这种行为,该时间线显示了Activity之间的进度以及每个时间点的当前返回栈。

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:一个如何在任务中将每个新的Activity添加到返回栈中的展示。当用户按下“返回”按钮时,当前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.

如果用户继续按下返回键,然后每个在栈中的Activity就会弹出并显示被弹出Activity的前一个(Activity),直到用户返回到主屏幕(或者在任务开始时运行的任何活动)。当所有的Activity从栈中移除时,该返回栈不再存在。

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 app from the app launcher. When the Home screen appears, Task A goes into the background. When the new app starts, the system starts a task for that app (Task B) with its own stack of activities. After interacting with that app, the user returns Home again and selects the app 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 app icon that started that task (or by selecting the app's task from the Recents screen). This is an example of multitasking on Android.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值