`
小怪兽lee
  • 浏览: 5127 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Activity and Tasks

阅读更多
Android的基本概念很重要啊
Activities and Tasks

As noted earlier, one activity can start another, including one defined in a different application. Suppose, for example, that you'd like to let users display a street map of some location. There's already an activity that can do that, so all your activity needs to do is put together an Intent object with the required information and pass it to startActivity(). The map viewer will display the map. When the user hits the BACK key, your activity will reappear on screen.

To the user, it will seem as if the map viewer is part of the same application as your activity, even though it's defined in another application and runs in that application's process. Android maintains this user experience by keeping both activities in the same task. Simply put, a task is what the user experiences as an "application." It's a group of related activities, arranged in a stack. The root activity in the stack is the one that began the task — typically, it's an activity the user selected in the application launcher. The activity at the top of the stack is one that's currently running — the one that is the focus for user actions. When one activity starts another, the new activity is pushed on the stack; it becomes the running activity. The previous activity remains in the stack. When the user presses the BACK key, the current activity is popped from the stack, and the previous one resumes as the running activity.

The stack contains objects, so if a task has more than one instance of the same Activity subclass open — multiple map viewers, for example — the stack has a separate entry for each instance. Activities in the stack are never rearranged, only pushed and popped.

A task is a stack of activities, not a class or an element in the manifest file. So there's no way to set values for a task independently of its activities. Values for the task as a whole are set in the root activity. For example, the next section will talk about the "affinity of a task"; that value is read from the affinity set for the task's root activity.

All the activities in a task move together as a unit. The entire task (the entire activity stack) can be brought to the foreground or sent to the background. Suppose, for instance, that the current task has four activities in its stack — three under the current activity. The user presses the HOME key, goes to the application launcher, and selects a new application (actually, a new task). The current task goes into the background and the root activity for the new task is displayed. Then, after a short period, the user goes back to the home screen and again selects the previous application (the previous task). That task, with all four activities in the stack, comes forward. When the user presses the BACK key, the screen does not display the activity the user just left (the root activity of the previous task). Rather, the activity on the top of the stack is removed and the previous activity in the same task is displayed.

The behavior just described is the default behavior for activities and tasks. But there are ways to modify almost all aspects of it. The association of activities with tasks, and the behavior of an activity within a task, is controlled by the interaction between flags set in the Intent object that started the activity and attributes set in the activity's <activity> element in the manifest. Both requester and respondent have a say in what happens.

In this regard, the principal Intent flags are:

FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_SINGLE_TOP

The principal <activity> attributes are:

taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch

The following sections describe what some of these flags and attributes do, how they interact, and what considerations should govern their use.

翻译如下
    如前面所提到的,一个Activity可以启动另一个Activity,即使这个Activity是定义在另一个应用里的。假设,比如说,你想展示给用户一条街的地图,现在已经有一个Activity可以做这件事,那么现在你需要做的就是将你请求的信息放进一个Intent对象里,并且通过startActivity()传递给他,这个地图就可以显示出来了,但用户按下BACK键时,你的Activity又重新出现在屏幕上。

    对用户来说,显示地图的Activity和你的Activity好像在一个应用中的,即使是他们是定义在不用的应用中的,运行在各自的应用进程中,android将两个Activity放进一个task里,一个task是一组彼此联系的Activity,被安排在一个堆栈中,堆栈中的根 Activity就是开辟这个task的,一般的,他是用户选择应用后首先启动的那个Activity,堆栈顶部的Activity是当前正在运行的 Activity,当一个Activity启动另一个Activity时,新的Activity被压进堆栈中,成为运行的Activity,当用户按下 BACK键,当前的Activity弹出堆栈,先前的Activity恢复成为运行的Activity。

    一个task就是一组Activity的堆栈,不是在manifest文件里的一个类,一个元素,所以没有方法来为一个task里的Activity独立的设置值,对task设置值是在root Activity里设置的。

    一个task里的所有Activity组成一个单元,整个task(整个Activity堆栈)可以在前台,也可以在后台(应用程序的切换就是 task的前后台的切换),假设,当前的task有四个Activity在堆栈里,用户按下HOME键,去开启另一个应用(实际上是一个新的task),那么当前的task就退到后台运行,新开启的应用的root Activity此时就显示出来了,然后,过了一段时间,用户回到主界面,又重新选择了以前的那个应用(先前的那个task),那么先前的那个task此时又回到了前台了,当用户按下BACK键时,屏幕不是显示刚刚关闭的那个应用,而是移除回到前台的这个task堆栈栈顶Activity,将下一个 Activity显示出来。

    刚才描述的情况是Activity和task默认的行为,但是有很多的方法来对几乎所有的方面进行修改,如Activity和task的联系。 task里Activity的行为,是受启动它的Intent对象的flag和在manifest文件中的Activity的属性集合共同影响的。

Flag:

FLAG_ACTIVITY_NEW_TASK

FLAG_ACTIVITY_CLEAR_TOP

FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

FLAG_ACTIVITY_SINGLE_TOP

属性:

taskAffinity

launchMode

allowTaskReparenting

clearTaskOnLaunch

alwaysRetainTaskState

finishOnTaskLaunch
分享到:
评论

相关推荐

    Android中文开发指南

    6. **Activity和任务 (Activity and Tasks)** - **Activity**:Activity是用户与应用交互的界面,每个Activity可以视为一个屏幕或窗口。它们可以按栈的方式组织成任务(Task),形成应用程序的导航结构。 - **...

    Android开发指南中文版

    4. **Activity和任务 (Activity and Tasks)** - **Activity**存在于任务中,任务是一组按照后进先出(LIFO)顺序排列的Activity,用户可以通过返回键逐个退出。 - **Affinity(吸引力)和新任务**:Activity可以...

    Android开发指南中文版.pdf 清晰版

    **Activity和任务(Activity and Tasks)** Activity是应用程序中最核心的组件,用于显示界面和与用户交互。一个Activity可以启动另一个Activity,或者与之进行数据交换。任务是一系列的Activity集合,它们共同完成...

    Android开发指南全中文版

    **Activity和任务(Activity and Tasks)** - **Affinity(吸引力)和新任务(New Task):** Affinity决定了Activity加入哪个任务(Task),而新任务则创建一个新的任务来容纳Activity。 - **加载模式(Launch Mode):** 定义...

    Android编程指南(中文)

    Activity 和任务 - Activity and Tasks** - **Affinity 和新任务**: Affinity 控制着 Activity 在任务栈中的分组方式。新任务则指通过 Intent 启动一个新的任务栈。 - **加载模式**: Activity 的加载模式定义了 ...

    Android学习笔记-Activity篇

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

    C/C++,组合算法-K人活动选择问题(Activity-Selection-Problem)的源程序

    Activity Selection problem is a approach of selecting non-conflicting tasks based on start and end time and can be solved in O(N logN) time using a simple greedy approach. Modifications of this ...

    Android Programming: The Big Nerd Ranch Guide, 3rd Edition

    More About Intents and Tasks Chapter 25. HTTP and Background Tasks Chapter 26. Loopers, Handlers, and HandlerThread Chapter 27. Search Chapter 28. Background Services Chapter 29. Broadcast Intents ...

    Analyzing Neural Time Series Data图书

    By analyzing these signals, researchers can gain insights into how the brain processes information, makes decisions, and performs various cognitive tasks. #### Purpose of the Book The primary ...

    Linux Shell Scripting Cookbook - Third Edition

    Using the shell, you can generate databases and web pages from sets of files, automate monotonous admin tasks such as system backups, monitor your system's health and activity, identify network ...

    Android.Programming.The.Big.Nerd.Ranch.Guide.2nd.Edition.0134171454.epub

    More About Intents and Tasks Chapter 23. HTTP & Background Tasks Chapter 24. Loopers, Handlers, and HandlerThread Chapter 25. Search Chapter 26. Background Services Chapter 27. Broadcast Intents ...

    WORKFLOW AND PARALLELEXTENSIONS IN .NET FRAMEWORK 4

    #### Invoke activity under System.Threading.Tasks.Parallel and any Task APIs WF 活动可以在任何支持 TPL 的环境中调用,包括在 `System.Threading.Tasks.Parallel` 和其他基于 Task 的 API 中。这种方式使得 ...

    Microsoft SQL Server 2008 Administrator's Pocket Consultant.chm

    performing backups and recovery, replicating transactions, tuning performance, managing server activity, importing and exporting data, or performing other key tasks. Featuring quick-reference tables,...

    Microsoft SQL Server 2008 Administrator's Pocket Consultant

    performing backups and recovery, replicating transactions, tuning performance, managing server activity, importing and exporting data, or performing other key tasks. Featuring quick-reference tables,...

    Cisco Packet Tracer 7.2.2 x32

    Stidents can performconfiguration review or config tasks in the PTMO activity before answering the exam question. Older version of Packet Tracer can still be used using the traditional ways to ...

    Cisco Packet Tracer 7.2.2 x64

    Stidents can performconfiguration review or config tasks in the PTMO activity before answering the exam question. Older version of Packet Tracer can still be used using the traditional ways to ...

Global site tag (gtag.js) - Google Analytics