Launchmode analysis and Intent.flag_new_task explanation of "turn" activity

Source: Internet
Author: User

There are four types of activity activation modes, respectively:

1. Standard

2, Singletop

3, Singletask

4, SingleInstance

Here we discuss the four types of startup modes in two different ways: between app and app

Start by talking about in-app

Standard: Regardless of whether the activity exists in the current task stack, a new activity will be created, such as a B for the task stack, and a B for the task stack to start B

Singletop: If the activity currently being created is at the top of the task stack, then no new activity is created, only the Onnewintent method of the activity is called , if not at the top of the stack (or there is no activity in the stack) ), the new activity will still be created, such as the task stack for a B start B task stack changed to a b if you start a then the task stack is a b A (such as news push, 10 news push will only invoke one activity 10 pages is it annoying?) )

Singletask: If there is activity to start in the current task, then no new activity is created and new activity will be created if it does not exist, such as the task stack is a B C and start B, then the task stack becomes a B An activity that is always placed on top of the stack when the activity is started and destroyed on its stack (such as the Start Page, home (when returned))

SingleInstance: When an activity's launchmode is set to this value, it indicates that the activity occupies a single task queue, which is not allowed to join other activity (independent applications such as alarms)

Between the different apps:

Since there are different apps, then there are two task stacks, and there is a processing foreground, one running in the background

Standard: The activity of the current station initiates the activity of the background task, regardless of whether the activity already exists in the background task stack, creates a new activity and joins it in the foreground task stack

Singletop: If the activity of the front office starts the activity of the background task, and the activity is already at the top of the background task, and it is different from the app, it is not sure if a new activity will be created. It is also necessary to see if there is a intent.flag_activity_new_task in the intent that initiates the ACTIVITY, and if this tag, then the task queue in the background will be moved to the front end, such as the front-end task stack A B back-end Task stack E F C, Now the front-end needs to start a C, if there is intent.flag_activity_new_task, then the back-end task stack will be moved to the front (and call C onnewintent, see below flag_activity_new_task analysis), The front-end stack is relegated to the backend. Without this tag, just create a new activity C in the front-end task stack.

Singletask: If the activity of the foreground starts the activity of the background task, regardless of whether the activity is on top of the stack, the background task stack is moved to the foreground and the foreground task stack is moved to the background. There is no need to consider the tagging issue, because if the activity being initiated is singletask, then the above tag is automatically added to the intent that initiates the activity.

SingleInstance: If the foreground activity starts the activity of the background task, if the activity is already in the background task stack, then the activity's onnewintent is called, and the background task is still in the background. If the activity is not in the background task stack, a acitivyt is recreated and placed separately into a task stack, which is actually added to the intent that launches the acitivity.

A task is a stack that contains a series of activity, followed by the advanced post-out principle.

Task default behavior:

(1) Premise: Activity A and Activity B are in the same application.

Action: Activity A starts the secluded task stack (stack state: A), starts activity B (stack State: AB) in activity A, and presses the back return key (stack state: a).

(2) Premise: Activity A and Activity B in the same application, the application name is "TaskOne application".

Action: Click the TaskOne app icon in launcher, Activity A launches the secluded task stack, named Taska (Taska stack State: A),

Start Activity B (Taska stack Status: AB) in activity A, press and hold the home key, return to launcher, launch other applications (e.g. ebook),

Open secluded a new task stack, name: TASKB, press and hold home, return to launcher, click the TaskOne app icon, and the Taska stack returns to the foreground.

Activity B is a top-of-the-stack application for user use.

(3) Premise: Activity A in the name "TaskOne application" in the application, activity C in the name of "Tasktwo application" in the application.

Action: Click the TaskOne app icon in launcher, Activity A launches the secluded task stack, named Taska (Taska stack State: A),

Start Activity C (Taska stack Status: AC) in activity A, press and hold the home key, return to launcher, launch "Tasktwo app" i.e. activity C,

Open the secluded new task stack, name TASKB, press the back key to return to launcher, click the TaskOne app icon, and the Taska stack returns to the foreground.

Activity c is a top-of-the-stack application for user use.

Intent Flag Introduction:

(1) Flag_activity_new_task: Set this state, keeping in mind the following principles, first to find out if there is a task stack with the same affinity for the ACTIVITY being started (i.e. taskaffinity, Note that the affinity of the activity in the same application, so the following a case will be in the same stack , if there is, just directly put this stack to the foreground, and keep the state of the stack unchanged, that is, the activity sequence in the stack unchanged, if not, A new stack is created to hold the activated activity

A. Prerequisites: Activity A and Activity B are in the same application.

Action: Activity A starts the secluded task stack (stack state: A), starts activity B in activity A, and the flag of intent that initiates activity B is set to

Flag_activity_new_task, activity B is pressed into the stack (stack state: AB) where activity A is located.

Cause: By default, all activity in an app has the same relationship (taskaffinity).

B. Prerequisites: Activity A in an application named "TaskOne Application", activity C and Activity D are in an application named "Tasktwo app".

Action 1: Click the TaskOne app icon in launcher, Activity A launches the secluded task stack, named Taska (Taska stack State: A),

Starting activity C in activity A, the flag for intent with activity C is set to Flag_activity_new_task,

The Android system will open secluded a new task for activity C, named TASKB (TASKB stack State: C), press and hold the Home key, select Taska,

Activity a returns to the foreground and starts activity C again (two cases 1. starting from the desktop; 2. Starting from Activity A, as in both cases), when TASKB returns to the foreground, activity c is displayed for use by the user, i.e.:

A task that contains Flag_activity_new_task's intent start activity is running, and no new task is created for that activity.

Instead, the original task is returned to the foreground display.

Action 2: Click the TaskOne app icon in launcher, Activity A launches the secluded task stack, named Taska (Taska stack State: A),

Starting activity C in activity A, the flag for intent with activity C is set to Flag_activity_new_task,

The Android system will open secluded a new task for activity C, named TASKB (TASKB stack State: C), starting in activity C

Activity D (TASKB Status: CD) long Press the Home key, select Taska, activity a back to the foreground, start activity C again (starting from the desktop or Activitya, is the same),

At this time TASKB back to the front desk, Activity D is displayed for the user to use. After setting the Flag_activity_new_task in this case, it will first find out if there is a stack with Activity C, according to Affinity (taskaffinity) , if any, just move the stack to the foreground, and keep the state of the stack unchanged, that is, the order in the stack is unchanged.

(2) Flag_activity_clear_top:

Premise: Activity A, activity B, activity C and Activity D are in the same application.

Action: Activity A starts the secluded task stack (stack state: A), starts activity B in activity a (stack state: AB), starts in activity b

Activity C (Stack state: ABC), start activity D (Stack state: ABCD) in activity C, start activity B in Activity D,

The flag of intent that initiates ACTIVITY B is set to Flag_activity_clear_top, (stack state: AB).

(3) Flag_activity_brought_to_front:

If activity A is in an application named "TaskOne Application", activity C and Activity D are in an application named "Tasktwo app".

Action: Click the TaskOne app icon in launcher, Activity A launches the secluded task stack, named Taska (Taska stack State: A),

Starting activity C in activity A, the flag for intent with activity C is set to Flag_activity_new_task,

The Android system will open secluded a new task for activity C, named TASKB (TASKB stack State: C), starting in activity C

Activity D (Stack state of TASKB: CD), press and hold the Home key, select Taska, activity a returns to the foreground and activity C is activated again in activity a.

Set flag to Flag_activity_brought_to_front in intent starting ACTIVITY C, taskb back to the foreground,

Activity C shows, (TASKB stack state: C).

(4) Flag_activity_multiple_task:

Used in conjunction with Flag_activity_new_task, first set flag_activity_new_task in intent, open ACTIVITY,

Starts a new task, then sets Flag_activity_multiple_task in intent and opens the same ACTIVITY again, and a new task is started.

(5) Flag_activity_single_top:

There is an ABCD four activity in the current task stack, a is the top activity of the stack, and D is the bottom of the stack activity, which is set in the intent of open a

Flag_activity_single_top flag, the stack top A is used instead of the new new A.

(6) Flag_activity_reset_task_if_needed:

Typically used in conjunction with Flag_activity_clear_when_task_reset, if this property is set, the ACTIVITY will start in a new task or be taken to the top of an already existing task, The activity will then be loaded as the first page of the task. Will cause a task with the same affinity to the application to be in a suitable state (move activity to or from this task), or simply reset the task to its initial state

Flag_activity_clear_when_task_reset: Sets a restore point in the current TASK stack when a intent request with flag_activity_reset_task_if_needed starts the stack ( A typical example is when the user starts the app again from the desktop, and the restore point includes the application that will be erased. Application scenario: In the email program preview the picture, will start the picture viewing actvity, when the user leaves the email processing other things, and then next time from home to enter the email, we present to the user should be the last email session, not the picture viewing, This will not cause confusion to users.

Example: There is activity a, activity B, activity C, activity A starts the secluded task stack, named Taska (Taska stack State: A),

Start activity B (Taska stack state: AB) in activity A, then activity B starts Activity C (Taska stack State: ABC),

Start ACTIVITY C by setting the Flag_activity_clear_when_task_reset title in the intent so that there is a restore point in the Taska

When there is a intent request Taska stack that contains flag_activity_reset_task_if_needed (e.g. request ACTIVITY A)

The system will clear the activity above the restore point, and only AB is left on the Taska stack.

Taskaffinity Properties:

(1) The taskaffinity attribute should be used in conjunction with the FLAG_ACTIVITY_NEW_TASK flag and the allowtaskreparenting attribute, if only the taskaffinity attribute is used,

Please refer to the default behavior of the task above.

(2) combined with the Flag_activity_new_task logo:

A. Title: Activity A and Activity B in the same application, activity A and Activity B set different taskaffinity properties.

Action: Activity A starts the secluded task stack, named Taska (Taska stack State: A), starts activity B in activity A, starts activity b

Intent set the FLAG_ACTIVITY_NEW_TASK flag, the system will open secluded a task stack, TASKB (TASKB stack State: B).

B. Title: Activity A In "TaskOne application", Activity C in "Tasktwo app", activity A and ACTIVITYC set the same taskaffinity attribute.

Action: Activity A starts the secluded task stack, named Taska (Taska stack State: A), launches activity C in activity A, initiates activity C

When the FLAG_ACTIVITY_NEW_TASK flag is set in intent, activity C presses into the same Taska stack as the activity a stack (Taska stack state: AC).

(3) with allowtaskreparenting properties:

Example: In "TaskOne app" There is a weather report activity A, activity a and other activity in the "TaskOne app" have a default relationship

(The Taskaffinity property is not set), and the Allowtaskreparenting property is set to True, there is now a "Tasktwo app

"Launch the weather report in the TaskOne app" activity A, when activity A is in the same task as the activity in the "Tasktwo app",

The task stack is named Taska, when the "TaskOne application" starts and the weather forecast activity A is developed, and activity A is transferred from the Taska stack to

The stack where the "TaskOne app" is located, where activity a can be moved back and forth across multiple stacks.
Alwaysretaintaskstate Properties:

If root activity in the task stack sets this property value to true, the status of the activity in the task stack is preserved regardless of any situation.

Cleartaskonlaunch Properties:

If the root activity in the task stack sets this property value to True, the system will immediately clean up all activity except root activity as soon as you leave the task stack.

Finishontasklaunch Properties:

If an activity sets the Finishontasklaunch property, the system clears the activity as soon as you leave the task stack,

Regardless of whether the activity is anywhere on the stack.

Launchmode analysis and Intent.flag_new_task explanation of "turn" activity

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.