Relationship between intent flag and Startup Mode

Source: Internet
Author: User

Activity has four startup modes:

1. Standard (standard) 2. singletop 3. singletask 4. singleinstance

Identifies the Startup Mode of an activity in two ways:

1. One is through androidmanifest. xml 2. The other is identified by intent

It is identified by androidmanifest. xml:

  1. <Activity Android: Name = ". activity1"
  2. Android: launchmode = "standard"
  3. Android: Label = "@ string/app_name">
  4. <Intent-filter>
  5. <Action Android: Name = "android. Intent. Action. Main"/>
  6. <Category Android: Name = "android. Intent. Category. launcher"/>
  7. </Intent-filter>
  8. </Activity>

It is identified by the flag of intent:

Flag_activity_new_task

Flag_activity_single_top

Flag_activity_clear_top

Bytes ------------------------------------------------------------------------------------------------

Flag_activity_new_task --- there is no corresponding startup mode. Some of its features are similar to those of singletask.

Public static final int flag_activity_new_task

Added in API Level 1

If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move. tasks can be moved to the foreground and background; all of the activities inside of a special task always remain in the same order. see tasks and back stack for more information about tasks.

This flag is generally used by activities that want to present a "launcher" style behavior: They give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will be simply be brought to the front of the screen with the state it was last in. seeFLAG_ACTIVITY_MULTIPLE_TASKFor a flag to disable this behavior.

This flag can not be used when the caller is requesting a result from the activity being launched.

Constant Value: 268435456 (0x10000000)

Using the intent of this feature, the system creates a new task for the started activity and stores the started activity at the bottom of the rollback stack.

Lab:

1. In the same application:

Set the flag_activity_new_task identifier when acitivyt is started.

Intent newtask =NewIntent (mainactivity.This, Com. intentflag. activity. activitynewtaskflag.Class);

Newtask. addflags (intent.Flag_activity_new_task);

Startactivity (newtask );

Effect:

From the above two figures, we can find that their taskids are the same. This shows that when flag_activity_new_task is used for identification,

In the same application, a new task is started to store the started activity instance.

From the perspective of times worth changing, if the current task contains an instance of the activity to be started, the system will still create a new instance even if the flag_activity_new_task ID is used.

2. In two different applications:

1). When flag_activity_new_task is used

This is the application where the started activity is located:

This is the application where the initiator is located:

After successful startup:

Then, press the return key:

We can know that: 1. The started activity is in a different task from the started activity. 2. The system will create a new started activity instance.

3. The new activity is located at the top of a new task. If the task already exists, the activity instance is located at the top of the stack.

2). If flag_activity_new_task is not used:

As you can see from the results, if flag_activity_new_task is not used, the started activity will use the same task as the initiator, which is to create a new activity. Then, store the activity instance in the task used by the initiator.

To sum up, flag_activity_new_task is suitable for the activities that are not the same as those started by the app. An application uses a task to organize all its activities.

Bytes --------------------------------------------------------------------------------------------------------------------------

Flag_activity_single_top

Public static final int flag_activity_single_top

Added in API Level 1

If set, the activity will not be launched if it is already running at the top of the history stack.

Constant Value: 536870912 (0x20000000)

Use this identifier

Example:

The started activity is at the top of the stack in another application:

Then, in another application, use the flag_activity_single_top identifier to start:

Then, the result after the startup is:

After experiments, we found that there was no effect as stated above.

You can see that a new cactivity is created.

Else ---------------------------------------------------------------------------------------------------------------------------------

Flag_activity_clear_top

Public static final int flag_activity_clear_top

Added in API Level 1

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this intent will be delivered to the (now on top) Old activity as a new intent.

For example, consider a task consisting of the activities: A, B, C, D. if d cils startactivity () with an intent that resolves to the component of Activity B, then C and D will be finished and B receive the given intent, resulting in the stack now being: a, B.

The currently running instance of Activity B in the above example will either receive the new intent you are starting here in its onnewintent () method, or be itself finished and restarted with the new intent. if it has declared its launch mode to be "multiple" (the default) and you have not setFLAG_ACTIVITY_SINGLE_TOPIn the same intent, then it will be finished and re-created; for all other launch modes or ifFLAG_ACTIVITY_SINGLE_TOPIs set then this intentWill be delivered to the current instance's onnewintent ().

This launch mode can also be used to good effect in conjunctionFLAG_ACTIVITY_NEW_TASK: If used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. this is especially useful, for example, when launching an activity from the notification manager.

See tasks and back stack for more information about tasks.

Constant Value: 67108864 (0x04000000)

Example:

By default, only the flag_activity_clear_top identifier is used.

According to the description in the document, A-> B-> C-> D, start Activity B, and the identifier used is flag_activity_clear_top,

Then, other activities in the rollback stack will be cleared, and B will be destroyed and re-created.

The final result is: A --> B (newly created)

Lab:

Then, press the return key and the result is returned to a. This indicates that B, C, and D are all cleared, and the stack top is the new B.

This effect is the same as described in the document.

Relationship between intent flag and Startup Mode

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.