Activity Startup Mode and association analysis between Intent Flags and stack

Source: Internet
Author: User

In the process of learning Android, Intent is the most commonly used mechanism for Android communications within or between processes. Its underlying communication is implemented by the Binder Mechanism, in the physical layer, it is implemented through shared memory.Intent is mainly used in two scenarios: (1) Initiating Intent (2) BroadcastingIts attributes include ComponentName, action, data, category, extras, and flags. Generally, Intent matching involves three attributes: Action, Data, and Category. These things need to be understood in order to have a deep understanding of it.Next I will summarize and record the Activity Startup Mode and association analysis between Intent Flags and stack based on my recent study.

Article structure:
1. What is stack?2.Activity Stack3.Task4.Activity Startup Mode5.Contact Activity stack and Task6.Intent Flags7.Activity-related attributesTaskAffinity

1. What is stack?
Stack

Stack is a common data structure. The stack only allows access to the elements at the top of the stack, and the stack is like a cup. Each time, you can only take things on the top of the cup, for a stack, you can only access the top elements of the stack each time, so that other elements below the top elements of the stack can be protected. "first-in-first-out" or "later-in-first-out" is a major feature of the stack. The elements of the advanced stack always have to wait until the elements of the later-in-stack exit the stack. recursion uses the system stack to temporarily Save the temporary results and protect the temporary results.
 
Stack)Stack definition Stack is a linear table that only inserts and deletes operations at one end of the table. (1) This end is usually referred to as the Top and the Bottom of the stack ). (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 (LIFO. Stack modification is based on the principle of "back-to-first-out. Each deletion (rollback) is always the "latest" element in the current stack, that is, the last element to be inserted (into the stack), and the first element to be inserted is placed at the bottom of the stack, it cannot be deleted until the end.
Stack operations: Stack pressure and stack bounce 2. Stack in Activity
Android management is mainly implemented through the Activity stack. When an Activity is started, the system pushes it to a specific stack according to its configuration, and the system is running. When you click "return" or "FINISH ()", the Activity will be pushed out of the stack and destroyed accordingly. It can be known according to the lifecycle of the Activity, if the currently displayed stack Activity is not destroyed, the newly opened Activity will be pushed to the stack when it is opened, the original status change is selected based on the display situation (the original Activity is still visible and changes to the Paused State). If it is completely covered, it is changed to the Stopped state (Stopped )).
 
 
 
 
3. Task 

Task is an important concept related to Activity. It is closely related to the Activity stack. Simply put, it is a set of Activity components that gather together in the stack mode. (Only the Startup Mode of the Activity is mentioned here)
4. Activity Startup Mode
Property: android: launchMode Function: Used to indicate how the Activity is started. Description: There are four modes, which work with the FLAG_ACTIVITY _ * variable of the Activity Flags in the Intent object to determine how the Activity starts to process the Intent. Four TypesMode: "Standard "--Default Mode
"SingleTop"
"SingleTask"
"SingleInstance"

The following examples illustrate their differences:


Standard: The default Loading Method of an Activity. This method redirects to a new activity and pushes the instance to the stack (whether or not the activity already exists in the Task stack, all adopt the new operation ). For example, if the order in the stack is a B c d and D jumps to A through Intent, the structure in the stack is changed to a B C D, the display order of the return button is d c B A, which is destroyed in sequence.


SingleTop: In singleTop mode, if the current Activity D is located at the top of the stack and jumps to its own Activity (that is, D) through Intent, a new D instance will not be created, therefore, the stack structure is still a B c d. If you jump to B, because B is not at the top of the stack, A new B instance will be created and pushed to the stack, the structure is changed to a B c d B.


SingleTask:In singleTask mode, the Task stack can have only one instance corresponding to the Activity. For example, the current stack structure is a B C D. In this case, D jumps to B through Intent, and the stack structure is changed to a B. C and D are destroyed by the stack pop-up, that is to say, all instances on B are destroyed.


SingleInstance: In singleInstance mode, the opened Activity is pushed into a new task stack. For example, if the structure of Task Stack 1 Is a B C and C jumps to D through Intent (the mode of D is singleInstance), A new Task stack 2 is created, in stack 1, the structure is still a B C, and in stack 2, the structure is D. In this case, D is displayed on the screen, and D jumps to D through Intent. In stack 2, no new D is pushed, so the two stacks have not changed. If D jumps to C, it will perform corresponding operations in stack 1 according to C's corresponding launchMode. If C is set to standard, D jumps to C, the structure of stack 1 is a B c. Click the return button and change the structure of stack 1 to a B C instead of D.


5. Contact the Activity stack and Task
A Task is a set of Activity components that are aggregated in stack mode. Similar to a container that fills the Activity, the first Activity to be added is located at the bottom of the container, the Last part is at the top of the container, while the Activity retrieved from the Task is First taken from the top, and the Last part is to add the Activity at the beginning. This is the Last In First Out) mode, and the sequence of the Activity in the Task can be controlled. When the Activity jumps, Intent Flag can be used to set the creation method of the new activity (the use of Intent Flag is involved here ).
6. Intent Flags
Flags: indicates the Intent flag, which is often used in the Activity scenario. It is closely related to the Activity startup mode.
The following lists the Flags attributes related to the topic of this article:
Intent. FLAG_ACTIVITY_NEW_TASK (default)
The default jump type, which will re-create A new Activity. However, in this case, for example, if Task 1 has three Activity A, B, and C, if you start D in C, in AndroidManifest. if the Affinity value added to D in the xml file is different from that in the Task, the Activity will be added to the newly marked Affinity Task. If it is the default or the specified Affinity is the same as the Task, a new Activity is started in the same standard mode.
FLAG_ACTIVITY_SINGLE_TOP
This FLAG is equivalent to singletop in the startup mode. For example, the original stack structure is a B c d, and D is started in D. The stack structure is A, B, C, and D.
FLAG_ACTIVITY_CLEAR_TOP
This FLAG is equivalent to SingleTask in startup mode. The activities started by this FLAG will bring up all the activities on the Activity to be started to the stack space. For example, in the original stack, the structure is a B c d, and the jump from D to B is changed to a B. (This method can be used to close multiple activities, which will be mentioned in a later blog)
FLAG_ACTIVITY_BROUGHT_TO_FRONT
Many people write this online. If an activity exists in a task, it gets to the top and does not start a new Activity. This may mislead you! This FLAG actually means this! For example, if I have A and start B in A, add this flag to Intent in. In this case, B is started in FLAG_ACTIVITY_BROUGHT_TO_FRONT mode, and C and D (normally C and D) are started in B. If B is started in D at this time, at this time, the final stack is A, C, D, and B. If A, B, C, and D are started normally, whether or not B is started with FLAG_ACTIVITY_BROUGHT_TO_FRONT or not, if B is started in D, it will still become A, C, D, and B.
FLAG_ACTIVITY_NO_USER_ACTION
OnUserLeaveHint () is part of the activity cycle. It is used when the activity is returned to the background because the user wants to jump to another activity. For example, when you press the Home key, it will be called. For example, if a phone call comes in (not selected by the user), it will not be called. Then, how does the system differentiate whether the current activity is used to return to the background?
It is determined based on whether FLAG_ACTIVITY_NO_USER_ACTION exists in the Intent of the newly started activity that prompts the current Activity to return to the background.
Note: Call finish () so that this function is not called when the activity is destroyed.
FLAG_ACTIVITY_NO_HISTORY
This means that the Activity started with this FLAG will not exist in the stack once it exits, for example! It turns out to be A, B, C. At this time, C starts D with this FLAG, and D starts E again. At this time, the stack is A, B, C, E.
7. Activity-related attributes TaskAffinity
The android: taskAffinity attribute in the Activity is described as follows:
Activity is an affinity of a Task. An Activity with the same affinity theoretically belongs to the same Task (from the user's perspective, it is the same "application "). The affinity of a Task is determined by its root Activity.
Affinity determines two things: the re-host Task of the Activity (refer to the allowTaskReparenting feature) and the Task of the Activity host started with the FLAG_ACTIVITY_NEW_TASK flag.
By default, all activities in an application have the same affinity. You can set this feature to reorganize them, or even place the Activity defined in different applications into the same Task. To make it clear that the Activity does not host a specific Task, set a null string for this feature.
If this feature is not set, the Activity inherits from the application settings (refer to the taskAffinity feature of the <application> element ). The default affinity of the application is the package name set in the <manifest> element.

Note: For android: taskAffinity, this attribute of the Activity takes effect only when the Intent with FLAG_ACTIVITY_NEW_TASK is started, the system switches tasks with the same Task affinity to the foreground, and then starts the Activity. Otherwise, the Activity is still running in the Task that starts it.

The above summarizes the Activity startup mode and the association analysis between Intent Flags and stack, so that you can understand and use the jump mode in the next 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.