Tasks and Back Stack

Tasks and Back Stack

An application usually contains multiple activities. Each activity should be designed around a specific kind of action the user can perform and can start other activities. An activity can even start activities that exist in other applications on the device.

A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a task (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 Hone 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 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.

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 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. A task can then return to the "foreground" so users can pick up where they left off.

Multiple tasks can be held in the background at once. However, if the user is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost.

To summarize the default behavior for activities and tasks:

a> 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 button while in Activity B, Activity A resumes with its state restored.

b> When the user leaves a task by pressing the Home button, 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.

c> If the user presses the Back button, 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.

d> Activities can be instantiated multiple times, even from other tasks.

Saving Activity State

When the system stops one of our activities (such as when a new activity starts), the system might destroy that activity completely if it needs to recover system memory. When this happens, information about the activity state is lost. If this happens, the system still knows that the activity has a place in the back stack, but when the activity is brought to the top of the stack the system must recreate it (rather than resume it). In order to avoid losing the user's work, we should proactively retain it by implementing the onSaveInstanceState() callback methods in our activity.

Managing Tasks

The way Android manages tasks and the back stack, by placing all activities started in succession in the same task and in a "last in, first out" stack --- works great for most applications and we shouldn't have to worry about how our activities are associated with tasks or how they exist in the back stack.  However, we might decide that we want to interrupt the normal behavior.

We can do these things and more, with attributes in the <activity> manifest element and with flags in the intent that we pass to startActivity().

In this regard, the principal <activity> attributes we can use are:

taskAffinity, launchMode, allowTaskReparenting, clearTaskOnLaunch, alwaysRetainTaskState, finishOnTaskLaunch

And the principal intent flags we can use are:

FLAG_ACTIVITY_NEW_TASK, FLAY_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP

Most applications should not interrupt the default behavior for activities and tasks. If we determine that it's necessary for our activity to modify the default behaviors, use caution and be sure to test the usability of the activity during launch and when navigating back to it from other activities and tasks with the Back button. Be sure to test for navigation behaviors that might conflict with the user's expected behavior.

Defining launch modes

Launch modes allow us to define how a new instance of an activity is associated with the current task.

We can define different launch modes in two ways, Using the manifest file and Using Intent flags. If both activities define the new activity should associate with a task, then the way,Using Intent flags, is honored over the other way.

Some launch modes available for the manifest file 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.

Using the manifest file

When declaring an activity in our manifest file, we can specify how the activity should associate with a task using the <activity> element's launchMode attribute. The launchMode attribute specifies an instruction on how the activity should be launched into a task. There are four different launch modes we can assign to the launchMode attribute:

"standard" (the default mode)

"singleTop"

The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances, but only if the activity at the top of the back stack is not an existing instance of the activity.

When a new instance of an activity is created, the user can press the Back button to return to the previous activity. But when the existing instance of an activity handles a new intent, the user cannot press the Back button to the state of the activity before the new intent arrived in onNewIntent().

"singleTask"

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

Although the activity starts in a new task, the Back button still returns the user to the previous activity.

"singleInstance"

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

The behaviors that we specify for our activity with the launchMode attribute can be overridden by flags included with the intent that start our activity.

Using Intent flags

When start an activity, we can modify the default association of an activity to its task by including flags in the intent that we deliver to startActivity(). The flags we can use to modify the default behavior are:

FLAG_ACTIVITY_NEW_TASK

Start the activity in a new task. If a task is already running for the activity we are now starting, the task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent(). This produces the same behavior as the "singleTask" launchMode value.

FLAG_ACTIVITY_SINGLE_TOP

If the activity being started is the current activity (at the top of the back stack), then the existing instance receive a call to onNewIntent(), instead of creating a new instance of the activity. This produces the same behavior as the "singleTop" launchMode value.

FLAG_ACTIVITY_CLEAR_TOP

If the activity being started is already running in the current task, then instead of launching a instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent().

There is no value for the launchMode attribute that produces this behavior.

FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.

If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard".

Handling affinities

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. However, we can modify the default affinity for an activity.

We can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.

The taskAffinity attribute takes a string value, which must be unique from the default package name declared in the <manifest> element, because the system uses that 

name to identify the default task affinity for the application.

The affinity comes into play in two circumstances:

a> When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.

b> When an activity has its allowTaskReparenting attribute set to "true"

Clearing the back stack

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.

There are some activity attributes that we can use to modify this behavior: alwaysRetainTaskState, clearTaskOnLaunch and finishOnTaskLaunch.

Starting a task

We can set up an activity as the entry point for a task by giving it an intent filter with "android.intent.action.MAIN" as the specified action and "android.intent.category.LAUNCHER" as the specified category.

An intent filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way to launch the activity and to return to the task that it creates any time after it has been launched.

This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值