您的位置:首页 > 移动开发 > Android开发

Android 开发文档 程序基础——Activities and Tasks

2010-06-19 10:56 567 查看
正如前文所说,一个activity可以运行另一个,包括另一个程序中定义的。

对用户来说,看起来似乎这个其他程序的activity是你的程序的一部分,即使它由其他程序定义,并运行在其他程序进程中。android通过让
所有的actiivty在同一个Task中来实现这个用户体验。

简单的说,Task对用户来说就是一个“程序”,是一组相互联系的activity排列在一个stack中。stack中的root
activity是开始这个task的activity。一般来说就是用户在程序启动器中选择的activity。正在运行或者用户正在操作的
activity处在stack顶端。当一个activity启动了另一个,新的activity就被放在了stack的上面,变成了运行中的
activity。前一个保持在stack中。当用户按了back键,前端的activity就弹出了stack,前一个activity继续成为运行中
的activity。

stack含有对象,所以如果一个task有一个以上同一个activity子类的实例,例如多地图,stack会给每个实例提供独立的入
口?entry。

stack中的activity从不重新排列,只是推入或弹出。

task是activity的stack,不是类或者manifest中的组件。所以没有办法单独给tactivity的task赋值。整体赋给
task的值实际上是给了root activity。

task中所有的activity作为一个unit移动。task可以完整的被放在前台或者后台。

这儿有一个比较重要的例子

假如有一个含有四个activity的task,一个在前端,三个在后面。用户按了home键,来到程序启动器,选择了一个新的程序,也就是一个新
的task。现在的task就转到了后台,而新task的root
activity显示了出来。然后,一段比较短的时间后(可能时间长了前面的任务就自动结束了,译者buhutuu.cn
注),用户回到了主屏幕,再次运行之
前的程序。这时含有四个activity的task转到了前台。当用户按back键,屏幕并不会显示用户刚刚离开的activity,也就是第二个
task的 root activity,而是将现在这个stack顶端的activity移除,将这个stack的前一个activity显示出来。

task和activity的行为都是改变的。task中的activity的相互联系,以及activity的行为,都由
动activity的Intent对象

manifest中activity元素属性


互影响

进行控制。

转自我的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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: