Influence of Android Startup Mode on activity Behavior

Source: Internet
Author: User

Http://515632.blog.51cto.com/505632/714705

In addition to the startup mode, the flags of intent and other attributes defined by activity in manifest also affect activity behavior.

First, we must understand the following concepts.

I. task: the task mentioned here is different from the task in the Android system. The latter prefers multi-process and multi-thread, and the tasks here are related to application and activity. This is an interactive interface with users and is one of the four main components of Android. What is confusing is the application, which is defined in manifest and consists of multiple activities. Tasks also cover multiple activities, which can belong to different applications. From the activity perspective, a task is a collection of activities to complete a task. These activities are arranged in a stack, which has the following concept: Back stack.

Ii. Back Stack: the stack formed by activity is arranged in the order of opening. We can also say that it is a task stack. If the user leaves the task for a long time (such as 30 minutes), the system will clear all the activities except the root activity in the stack. When the user returns again, only the root activity is in the stack.

Secondly, I will list the above three aspects for your reference:

I. Four startup modes: The launchmode attribute defined in manifest has a significant impact on the startup behavior of the activity.

Android: launchmode = ["standard" | "singletop" | "singletask" |
"Singleinstance"]

The default value is "standard ".

See previous translations: http://blog.csdn.net/lincyang/article/details/6826021

An activity is in "standard" or "singletop" Startup Mode and can be instantiated multiple times.

Once a new intent is assigned to the "standard" activity, a new instance responding to this intent will be created. Each instance processes a separate intent. Similarly, an instance of a "singletop" activity may also be created to manage the new intent. However, if the target task already has an instance of this activity at the top of the stack, the instance will receive this new intent (callback in onnewintent); the new instance will not be created. In other cases, for example, if an existing "singletop" activity instance is in the target task but not on the top of the stack, or it is not in the target task at the top of the stack, and a new instance will be created and pushed to the stack.

The "singletask" and "singleinstance" modes are only different in one aspect: the "singletask" activity allows other activities to be part of a task, and it is usually at the root of the task stack, other activities (which must be in the "standard" and "singletop" modes) can be loaded to this stack. On the other hand, the activity in singleinstance mode does not allow any other activity to be loaded to this task stack. It is the only activity in the task. If it starts another activity, the activity will be directed to different tasks, which is like marking flag_activity_new_task in intent. (LINC note: In general, manifest uses the "singletask" Startup Mode and flag_activity_new_task in intent to mark the behavior ).

2. The Six Flags related to the activity in intent are important:

Flag_activity_new_task

Flag_activity_single_top

Flag_activity_clear_task

Flag_activity_clear_top

Flag_activity_no_history

Flag_activity_reorder_to_front

1,
Flag_activity_new_task

Similar to singletask in startup mode.

2,
Flag_activity_single_top

Similar to singletop in startup mode.

3. flag_activity_clear_task

It must be used with flag_activity_new_task. The activity starts a new stack. the stack is cleared and the activity in the stack is destroyed.

4. flag_activity_clear_top

The four activities of a B c d are in the same stack. Use this flag to start B. The original B is called out, and the CD is clear. It cannot be used with flag_activity_single_top. It can be used with flag_activity_new_task.

5. flag_activity_no_history

When the activity is not visible, the activity will be removed from the stack without leaving any records.

6. flag_activity_reorder_to_front

Rearrange the activity order in the stack. For example, the stack has four activities, a B c d. Use this flag to start B. The original B is aroused to the top of the stack. Order after sorting: A c d B

3. Other properties of the activity:

Android: allowtaskreparenting = ["true"
| "False"]

Android: alwaysretaintaskstate = ["true"
| "False"]

Android: cleartaskonlaunch = ["true"
| "False"]

Android: finishontasklaunch = ["true"
| "False"]

Android: nohistory = ["true"
| "False"]

Android: taskaffinity = "string"

These attributes are defined in manifest. The following describes them respectively.

1. Let's talk about the taskaffinity attribute: kinship of a task. The set string should be another task name, usually the name of an application. Activity with the same taskaffinity belongs to the same task and belongs to the same application from the user's perspective ). The kinship of a task depends on the Root Activity of the task.

It is a very important attribute. It will affect activity behavior together with other attributes and flag. First, it works with the "singletask" mode or flag_activity_new_task flag. After being started, this activity will belong to another task. The task name is the set value of taskaffinity. Secondly, it will be used with the allowtaskreparenting attribute. For details, see allowtaskreparenting.

By default, all activities in an application belong to one kinship. If you want to make the activity not belong to any task, set the taskaffinity value to null. If you do not set the package name, the package name in manifest is the default application name.

2. allowtaskreparenting: similar to literal comprehension, this attribute allows the activity to re-specify a task. The default value is false. Normally, this attribute is not set. The activity belongs to only one task in the lifecycle. After being set to true, it must be used with the taskaffinity attribute. When the original task is transferred to the background and the relative task is transferred to the foreground, the activity will belong to the current task.

3. alwaysretaintaskstate: always retains the task status. The default value is false. In this case, the system will clean up the stack. If it is set to true, the system retains all the activity and its status in the stack. When the user returns again, it finds that everything is as early as possible.

4. cleartaskonlaunch: when the task is started again, the system clears all the activities except the root activity. The default value is false.

5. finishontasklaunch: This attribute is similar to the cleartaskonlaunch mentioned above, but it refers to a single activity rather than the entire stack. When it is set to true, the activity will not be displayed after the task is restarted. The default value is false. The priority is higher than allowtaskreparenting.

6. nohistory: if it is set to true, the activity will be removed from the stack without leaving the activity invisible. The default value is false. The above flag_activity_no_history behavior is the same.

Related Article

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.