Activity startup mode tasks and back Stack

Source: Internet
Author: User

Http://www.cnblogs.com/mengdd/archive/2013/06/13/3134380.html

A task is a collection of activities that a user needs to interact with when doing a job. These activities are placed in a stack (backstack) in the order in which they are opened.

An activity can even open other apps ' activity.

For example, your app needs to send an e-mail message, you can define a intent to perform the Send action, intent contain some necessary data, and then start the activity in another app to perform the action of sending the message, and then back to your activity when the message is finished.

Android will place these two activity in the same task to maintain a consistent user experience.

Activity pressure Stack

When the current activity launches a new activity, the new activity is pressed on top of the back stack and gets the focus.

The previous activity is still in the stack, but stopped.

When an activity is stopped, the system maintains its user interaction state.

When the user presses the back key, the current activity will be out of the stack (the activity will be destroyed), the previous activity will be restored, and its UI status will be restored.

When the activities in the stack pops up sequentially and the stack is empty, the task does not exist.

Multitasking:task in the background

The task is also a cohesive unit, and when the user opens a new task, or the home key returns to the desktop, the task becomes run in the background.

When the task is in the background, all of the activity is stopped and the back stack remains intact, and the task simply loses focus.

The task can go back to the foreground so that the user can return to the scene where they left off, the topmost activity will resume.

Multiple tasks can be persisted in the background, but the system may kill the background activity to recover the memory , which can result in the state of the activity being lost.

Save Activity status

When activity stops (stopped), the default behavior of the system is to save its state.

However, you can and should use a callback method to proactively preserve your state in case the activity is killed by the system and needs to be rebuilt.

When the system killed the activity in order to recover the memory, the status information of the activity is lost.

But the system still knows where the activity is in the back stack, and when the activity returns to the top of the stack, the system rebuilds (recreate) it instead of resuming it as before.

So in order not to lose the user's work, you need to implement onSaveInstanceState() methods to proactively save the data.

Manage task

The Android system manages the task and back stack in such a way that the activity is pushed into the stack in the order it was started, and if an activity is started multiple times, it creates more than one instance of it and then presses the new instance into the stack.

If you want to intervene and change this default behavior, you can.

For example, you might want to start a task (instead of putting it in the current task) when one of your activity starts.

Or, when you start an activity, you want to refer to its existing instance to the front (instead of creating a new instance at the top of the stack);

Or, you want to clear all activity except the root activity when the user leaves your task.

To do this, you can do this through the <activity> properties of the elements in the manifest, and also through the intent flag that is passed in the settings startActivity() .

You can use the <activity> properties of:

    • Taskaffinity
    • Launchmode
    • Allowtaskreparenting
    • Cleartaskonlaunch
    • Alwaysretaintaskstate
    • Finishontasklaunch

And the flag of the main intent:

    • Flag_activity_new_task
    • Flag_activity_clear_top
    • Flag_activity_single_top
Defining the startup mode

The startup mode allows you to define how a new activity instance is associated with the current task.

You can define the startup mode in two ways: defined in manifest or by using intent flag.

  Here are two points to note:

1. Some boot modes can only be defined in manifest, and some can only be defined by the intent flag.

2. When an activity a starts an activity B, if B defines the startup mode in its manifest, a through intent requires it to use a different startup mode, which is the main requirement for a.

Using the manifest file to define the startup mode

You can <activity> use attributes to define how activity and task associations are used under tags in the manifest file launchMode , which specifies how activity starts and enters a task.

Note that the startup mode defined in manifest can be overwritten by the flag-marked pattern in the intent of the activity being initiated.

There are several property values:

"Standard" (the default mode)

The default mode. In the current task, a new instance of the activity is created and passed intent to it. An activity can be instantiated several times, each instance can belong to a different task, and a task can have multiple instances of it.

"Singletop"

If there is already an instance of this activity at the top of the current task, the system onNewIntent() passes the intent to this instance by means of the method, rather than re-establishing an instance of the activity.

An activity can be instantiated multiple times, and an instance can belong to a different task, and a task can have multiple instances of it (the activity at the top is not instantiated when it is an instance of the activity).

"Singletask"

The system will recreate a task and instantiate the activity, placing it at the root of the task.

However, if an instance of the activity is already present in a separate task, the system will call the onNewIntent() method to pass the new intent to the existing instance instead of recreating the instance.

At any one time, only one instance of this activity exists.

"SingleInstance"

Similar to "Singletask", the only difference is that the system does not initiate any other activity in the task where the activity instance resides.

The instance of this activity is always the only member of the task, and any other activity initiated by the activity will be opened in another task.

return processing

The return key always takes the user to the previous activity, regardless of whether the activity is started on a new task or the current task is started.

  But there is a special case: If you start an activity with a startup mode of Singletask, if the activity exists in a background task, then the entire task will be placed in the foreground, and back The stack will contain all the activities in this task, and they are placed on top of the stack.

Such as:

Use intent flag to indicate boot mode

By setting the flag passed to startActivity() the intent, you can modify the association mode of the activity to start and its task.

The flags that can be used are:

Flag_activity_new_task

The same as the "Singletask" discussed earlier, start the activity in a new task, and if a task of activity you need already exists, push it to the foreground, restore its previous state, and it passes the onnewintent () Receive this new intent.

Flag_activity_single_top

As with the "singletop" behavior, if the activity being started is the activity at the top of the current, the instance that already exists is received instead of the onNewIntent() new instance.

Flag_activity_clear_top

If the activity that is started is already running on the current task, does not create a new instance of it, but destroys all other activities on it, and then onNewIntent() passes a new intent to this restored activity.

This behavior launchMode does not have a corresponding attribute value in the.

Note that if the activity's startup mode is "standard", it will itself be removed and a new instance will be started.

  This is because when the startup mode is "standard", a new instance must be created in order to receive the new intent.

Handling affinities

Affinity indicates which task the activity prefers to belong to.

By default, the activities of the same app tends to be in the same task. You can modify this behavior by using the <activity> tag taskAffinity .

For more information, see: API guides:tasks and back Stack

Http://developer.android.com/guide/components/tasks-and-back-stack.html

Clean back stack

  If a user leaves a task for a long time, the system cleans up all activities in the task, except for the root activity. When the user returns to this task, only the root activity is restored.

  

There are some properties of activity that you can use to change this behavior:

Alwaysretaintaskstate

If this property is set to true in the root activity of the task, then the default behavior described above does not occur, and the task will still maintain all activities, even after a long time.

Cleartaskonlaunch

If this property is set to true in the root activity of a task, each time the user leaves the task, the entire task is cleared to the root activity.

This way the user can always return to its original state, even if the time to leave is short.

Finishontasklaunch

This property is similar to the previous one, but it works on a single activity, not an entire task.

It can cause any activity to leave, including root activity.

When it is set to true, the activity only belongs to the task in the current session, and if the user leaves and returns, it will not appear again.

Open a task

You can do this by giving the activity a intent filter (the action is "Android.intent.action.MAIN", the category is "Android.intent.category.LAUNCHER"), Let this activity be the entry point for a task.

As follows:

<activity >    <intent-filter ... >        <action android:name= "Android.intent.action.MAIN"/>        <category android:name= "Android.intent.category.LAUNCHER"/>    </intent-filter> ...    </ Activity>

One such intent filter allows the activity's icon and label to be displayed at the start of the program, providing a way for the user to initiate the activity and, when it is started, the user can return to the task.

The second ability is important: the user must be able to leave a task and then return to it via activity launcher.

For this reason, two startup modes that allow activity to instantiate a task forever: "Singletask" and "singleinstance", should only have a action_main and category_launcher in the activity Use them when you filter.

Resources

API Guides:tasks and Back Stack

Http://developer.android.com/guide/components/tasks-and-back-stack.html

<activity> Tags:

Http://developer.android.com/guide/topics/manifest/activity-element.html

For startup mode, you can also see the blog post:

Http://www.cnblogs.com/fanchangfa/archive/2012/08/25/2657012.html

Activity startup mode tasks and back Stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.