`

Tasks and Back Stack

阅读更多
Even though the activities may be from different applications, Android maintains this seamless user experience by keeping both activities in the same 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.

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.

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 key, 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 key. As such, the back stack operates as a "last in, first out" object structure.

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.

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 key.

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).

•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 key while in Activity B, Activity A resumes with its state restored.
•When the user leaves a task by pressing the HOME key, 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.
•If the user presses the BACK key, 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.

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.

Launch modes allow you to define how a new instance of an activity is associated 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).

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.

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.

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.
分享到:
评论

相关推荐

    Android学习笔记-Activity篇

    六、Tasks and Back Stack: 1. 管理Task:Task是Android中的一系列Activity堆栈,代表用户的操作序列。每个Task有自己的Back Stack,用于存储按启动顺序排列的Activity。 2. 定义launch模式:在AndroidManifest.xml...

    进阶篇设计高效的应用导航

    系统会自动管理一个返回堆栈(Tasks and Back Stack)或历史屏幕列表,记录用户访问过的屏幕顺序。然而,在特定情况下,开发者可能需要重写返回键的默认行为,以适应应用的特定需求。例如,在应用中嵌入了网页浏览器...

    ciclo-vida-activity-android:理解活动生命周期的例子

    9. **任务和栈(Tasks and Back Stack)** 活动可以组织在任务(Task)中,每个任务都有一系列的回退栈。默认情况下,新启动的活动会被添加到当前任务的栈顶,但可以使用Intent标志(如FLAG_ACTIVITY_NEW_TASK)来...

    OpenStack Trove Essentials(PACKT,2016)

    We then look at how to provision databases in self-service mode, and how to perform administration tasks such as backup and recovery, and fine-tuning databases. At the end of the book, we will ...

    Mastering ROS for Robotics Programming - Second Edition[www.rejoiceblog.com].pdf

    chapter explains how to record and play back messages using rosbag and rqt_bag. Chapter 4, 3D Modeling and Simulation, constitutes one of the first steps in order to implement your own robot in ROS. ...

    The Indispensable PC Hardware Book - rar - part1. (1/7)

    Stack segment and stack pointer. Data segment DS and addressing. Addressing types and instruction coding. Programming at processor level: mnemonics and the assembler. Addressing types. ...

    Script Inspector 3 v3.0.30

    =) Programmers can finally focus on their tasks instead of waiting for (and sometimes fighting with) the external IDE’s to run, load the correct script, or jump to the correct line. Main features: ...

    基于SSM+mysql的企业人事管理系统源码数据库论文.doc

    The SSM stack is a popular choice for building web applications in Java due to its flexibility, modularity, and efficient handling of business logic and data persistence. The tags "毕业设计" and ...

    Android任务管理器源码.zip

    1. **任务与活动(Tasks and Activities)** - 在Android中,任务(Task)是一组相关的Activity堆栈,它们按照启动的顺序排列,最新的Activity位于栈顶。任务管理器负责维护这些Activity的生命周期。 2. **...

    Microsoft Library MSDN4DOS.zip

    9.6 Interrupt Tasks and Interrupt Procedures 9.7 Error Code 9.8 Exception Conditions 9.9 Exception Summary 9.10 Error Code Summary Chapter 10 Initialization 10.1 Processor State After Reset 10.2 ...

    计算机网络第六版答案

    23. The five layers in the Internet protocol stack are – from top to bottom – the application layer, the transport layer, the network layer, the link layer, and the physical layer. The principal ...

    获取系统topActivity demo

    Android应用是基于任务(Task)和栈(Back Stack)的概念运行的,Activity会被压入栈中,当用户切换到其他Activity时,新Activity会被压入栈顶,而原来的Activity则被压在下面。因此,栈顶的Activity就是当前用户...

    获取栈顶的activity

    对于大多数常规应用需求,如导航和回退,Android框架已经提供了标准的处理方式,如使用Intent和back stack。只有在特定场景下,如全局监听或调试,才需要考虑直接获取栈顶Activity。 总的来说,理解和掌握如何获取...

    带优先级的多任务管理模块(C)

    If the task fails, the first move to a place, over time retry the put back, and so on ... There are many possible places to use it ... so, still retained 2. How many tasks can be complicated to ...

Global site tag (gtag.js) - Google Analytics