"Android Basics" Activity startup mode and full parsing of intent Flags and stacks

Source: Internet
Author: User

in the process of Android development, intent is our most commonly used Android mechanism for in-process or inter-process communication.
Intent is mainly used in 2 scenarios: initiating intent, broadcasting

Its underlying implementation principle is not covered in this article, will be updated in the future.
I will summarize the Activity Initiation mode and the association analysis of Intent Flags and stacks according to the recent study.

1, first we first understand what is the stack:

Stack is a common data structure, the stack only allows access to the top of the stack of elements, the stack is like a cup, each time can only take the top of the cup, and for the stack can only access its top stack each time, so that the protection of the top element of the stack below the other elements. "Advanced after-out" or "LIFO" is a big feature of the stack, the elements of the advanced stack will always wait until the elements of the backward stack after the stack can be out of the stack. Recursion is the use of the system stack, temporarily save temporary results, to protect the temporary results.

Definition stacks (Stack)
A stack is a linear table that restricts the insertion and deletion of operations only at one end of the table.
(1) The end of the insert, delete is usually called the top of the stack (top), the other end is called the bottom of the stack (Bottom).
(2) When there are no elements in the table, it is called an empty stack.
(3) The stack is a linear table of last-in, first-out, or LIFO table. Stack modification is based on the principle of last-in-first-out. Each deletion (fallback) is always the "newest" element in the current stack, which is the last element to insert (the stack), and the first inserted is placed at the bottom of the stack, to be deleted at the end.

Stack operation: Press stack, bounce stack

Stacks in 2.Activity

Android management is mainly through the activity stack, when an activity starts, the system will press it into a specific stack according to its configuration, the system is in a running state. When the user clicks back or finish () the activity, then it will be ejected from the top of the stack, then destroyed, according to the activity life cycle can be known, if the current display of the stack activity is not destroyed, then open the new activity, The newly opened press is pressed into the top of the stack, and the original activity is selected according to its display status (the original event is still visible, becomes a paused state (Paused), and if it is completed, it is turned into a stop state (Stopped)).

3.Task

Task is an important concept associated with activity that closely links the activity stack, which is simply a collection of activity components that are clustered together in a stack of patterns. (Just mention it and the activity's startup mode)

4.Activity Boot Mode

Property: Android:launchmode
Role: Used to indicate how activity starts.
Description: There are four modes that work with the properties of the activity flags in the intent object (the flag_activity_* variable) to determine how the activity starts to handle intent.
Four types of modes:

"Standard" – Default mode
"Singletop"
"Singletask"
"SingleInstance"

Standard: Activity's default Load method, if the Android:launchmode property value is not set, the system defaults to this value, the method will jump to a new activity, At the same time, the instance is pushed to the top of the stack (regardless of whether the activity already exists in the task stack, the new operation is used). For example: The order in the stack is a B c D, at this time D through intent jump to a, then the stack structure becomes a B c D A, click the return button to display the order is D C B A, in turn destroy.

singletop: singletop mode, the current Activity d at the top of the stack, if through intent jump to its own activity (that is, d), then will not re-create a new D instance, so the structure of the stack is still a B C D, if jump to B, then because B is not at the top of the stack, so a new B instance will be created and pressed into the stack, the structure becomes a B C D (you can simply understand that the role of this pattern is to ensure that the top of the stack can not have the same instance).

singletask: In singletask mode, there can be only one instance of the activity in the task stack. For example, the stack is now structured as: A B C D. At this point D jumps to B through intent, then the structure of the stack becomes: a B. C and D are destroyed by the stack eject, which means that instances above B are destroyed. that is to say: Singletask mode of activity, whether on the top of the stack or the bottom of the stack, when the activity is run again, it will destory off its activity to ensure that the entire stack only a self, (Note that this does not destroy an instance of the ACTIVITY at this point is different from flag_activity_clear_top) Remember that "this is beyond doubt."

singleinstance:in singleinstance mode, the open activity is pressed into a new task stack. For example: The structure of Task Stack 1 is: a b C, c jump to D through intent (d mode is singleinstance), then a new task stack 2 is created, the structure of the stack 1 is still a B C, and the structure of the stack 2 is D, when the screen shows D, If D is then redirected through intent to D, Stack 2 does not press into the new D, so the situation in the 2 stacks does not change . If d jump to C, then will be based on the C corresponding launchmode in the stack 1 in the corresponding operation, c if the standard, then D jump to C, the structure of the stack 1 is a B c C, at this time click the Back button, or C, the structure of the stack 1 into a B c, and not go back to D.

5.Activity Stack and task contact

Task is simply a collection of activity components clustered together in a stack of patterns, similar to a container filled with activity, the first activity to be placed at the bottom of the container, the last to be added to the top of the container, The activity from the task is removed from the top, and finally the activity is added at the very beginning, which is the LIFO (last on first out) mode, and the order of the activity in the task can be controlled, Intent flag can be used to set up new activity when the activity jumps (this involves the use of intent flag).

6.Intent Flags

Flags: Represents the intent flag, commonly used in the activity scene, and it is closely related to the activation mode of the activity.

Affinity (attraction) and new tasks

By default, activity in an application has a affinity── between each other, which means that their preference belongs to a task. However, each activity's Taskaffinity property can be set to a separate affinity in the element. Activity defined in different applications can then have the same affinity, or the activity defined in the same application has different affinity. Affinity takes effect in two cases: when the intent object that loads the activity contains the flag_activity_new_task tag, or when the activity's Allowtaskreparenting property is set to "true".

Flag_activity_new_task Mark

As mentioned earlier, by default, a new activity is loaded into the task by another activity that calls the StartActivity () method. and presses into the stack where the caller resides. However, if the intent object passed to StartActivity () contains a flag_activity_new_task tag, the system will schedule another task for the new ACTIVITY. In general, this is a new task, as the markup implies. However, this is not inevitable. If there is a task that has the same affinity as the new activity, the activity will be loaded into that task. If not, the new task is enabled.

Allowtaskreparenting Property

If an activity sets the Allowtaskreparenting property to "true". It can be transferred from the initial task to the task of having the same affinity and turning to the foreground. For example, a travel application contains an activity that predicts weather conditions for a selected city. It has the same affinity (default affinity) as other activity in the application and allows the parent to be re-determined. Your other activity starts the weather forecast, so it will be in the same mission as the activity. However, when the travel app returns to the front desk again, the weather forecast activity will be re-assigned to the original task and displayed.

If, in the user's view, an. apk file contains more than one "application", you may want to schedule a different affinity for their activity.

The flags properties related to this topic are listed below:

Flag_activity_new_task (default)

The default jump type, which re-creates a new activity, but with this case: for example, there are a,b,c three activity in Task1, when D is started in C, If the value added to D in the Androidmanifest.xml file is different from the affinity in the task, the activity is pressed into the task that exists in the newly tagged affinity. If the default or specified affinity and task are the same, start a new activity just like the standard mode.

Flag_activity_single_top

This flag is equivalent to the singletop in the startup mode, for example: the original stack structure is a B C D, start D in D, the case in the stack is still a,b,c,d.

Flag_activity_clear_top

This flag is somewhat similar to the Singletask in the startup mode, Note here that it is similar to ****singletask is not destroying this instance, and Flag_activity_clear_ Top is related to the startup mode of the newly launched activity, which, by default (standard) is the top of the stack that is destroyed and then recreated, and other modes are started in the other mode. Singletask will not destroy an ACTIVITY instance, Flag_activity_clear_top will eject it, but destroy itself, and recreate the new object. The combination of singletop and flag_activity_clear_top can achieve singletask effect.

Flag_activity_brought_to_front

If this flag is set and the activity being activated is already running, the activity will be transferred to the top of the stack.

For example, there are 4 activity:a,b,c,d in a task. If D calls StartActivity () to start B using this flag, then B will be transferred to the stack top of the history stack, the result order: A,c,d,b, otherwise the order will be: A,b,c,d,b. If the flag flag_activity_clear_top is used, the FLAG_ACTIVITY_REORDER_TO_FRONT flag is ignored.

Flag_activity_no_user_action

Onuserleavehint (), as part of the activity cycle, is used when the activity is returned to background because the user wants to jump to another activity. For example, when the user presses the home key, it will be called. For example, if a phone comes in (not a user's choice), it will not be called.
So how does the system differentiate between making the current activity go back to background and using it as the user's choice?

It is determined by whether there is flag_activity_no_user_action in the intent of the newly activated activity that prompted the current activity to retreat to background.

Note: Calling finish () does not call this function when it destroys the activity

Flag_activity_no_history

It means that with this flag activated activity, once exited, it will not exist in the stack, for example! The original is a,b,c this time again C with this flag to start D, D and then start E, this time the stack in the case of A,b,c,e.

7.Activity related Properties taskaffinity

The Android:taskaffinity property in Activity describes:

Activity is a affinity owned by a task. Activity with the same affinity is theoretically the same task (the same "application" in the user's perspective). The affinity of a task is determined by its root activity.
Affinity determines that two things--activity the task of the re-host (reference allowtaskreparenting attribute) and the ACTIVITY host initiated with the FLAG_ACTIVITY_NEW_TASK flag.
By default, all activity in an application has the same affinity. You can set this feature to reorganize them, and you can even put the activity defined in different applications into the same task. To make it clear that the activity does not host a particular task, set the string with an empty attribute.
If this feature is not set, the activity will inherit from the application's settings (refer to the taskaffinity attribute of the element). The name of the application default affinity is the package name set in the element.

Note: This property of the activity will only take effect when the activity is started by the android:taskaffinity of the Flag_activity_new_task intent. The system will switch a task with the same task affinity to the foreground and then start the activity, otherwise the activity is still running in the task that started it.

"Android Basics" Activity startup mode and full parsing of intent Flags and stacks

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.