Intent contains Intent and implicit Intent. Intent is usually used in the program and the Activity name is specified. intent = new Intent (context, Activity name. class); implicit Intent is generally used for interaction between different applications. Intent is often used in development. After Intent is started, tasks are enabled by the system to manage activities in the form of advanced and Outgoing Operations, in addition, the default management mode is to instantiate the Activity every time it is enabled. If you want to change the default Management Mode to better meet the development needs, you need to make some settings.
Below are some of my study notes: There are three aspects: 1. Intent Flag; 2. LaunchMode; 3. <activity> attribute in Manifest
I. Intent Flag
Intent Flag only learns the flags that can change the Task management mode. This is a detailed description of the history in the official document;
FLAG_ACTIVITY_BROUGHT_TO_FRONT
This identifier is generally not set by the program code, but set by the system when the launchMode is set to singleTask.
Prerequisites: Activiy A, B, C, and D are in the "FlagTest application ".
Scenario: In Launcher, click the "FlagTest application" icon. Activity A starts the uncommon Task Stack and name it TaskA (TaskA stack status: A). Activity A starts Activity B, in this case, the launchMode of Activity B is set to singleTask, Activity B starts Activity C, Activity C starts Activity D (the status of the TaskA stack is ABCD), and Activity D starts Activity B, because the start mode of Activity B is singleTask, the system will set FLAG_ACTIVITY_BROUGHT_TO_FRONT for Activity B. Then, the status of the TaskA stack is AB.
FLAG_ACTIVITY_CLEAR_TASK
If the intent that sets this identifier is enabled, the identifier is cleared before the activity is started. The result is that this activity becomes the root activity of another empty stack, and other old activities are destroyed. This can only be used with FLAG_ACTIVITY_NEW_TASK.
FLAG_ACTIVITY_CLEAR_TOP
If this identifier is set and the Activity is already running on the current stack, other activities on the Activity will be destroyed instead of starting a new instance of the Activity, then the intent will be distributed to the old Activity (at the top of the stack)
Premise: Activity A, B, C, and D in the same application
Scenario: Activity A starts the uncommon Task Stack (Task Stack status: A), Activity A starts Activity B, Activity B sets the FLAG_ACTIVITY_CLEAR_TOP identifier, and Activity B starts Activity C, activity C starts Activity D (the status of the TaskA stack is ABCD). Activity D starts Activity B. In this case, C and D are destroyed, and the status of the Task stack is AB.
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
If this identifier is set, the Activity in the stack marks a restore point that should be cleared when the stack is reset. That is to say, when the next stack carries the FLAG_ACTIVITY_RESET_IF_NEEDED identifier back to the foreground (this is a typical result of the user restarting from the main interface ), the Activity with the Restore Point Set and the Activity on this Activity will be destroyed, and the user cannot return to them, but can return to the Activity before
This is suitable for programs with Split points. For example:
Scenario Demonstration: When Activity A is started, the Task stack is opened (the status of the Task stack is A). Activity B may be started to display the image when Activity A is browsing the image, at this time, the Task stack status is AB. When the user leaves the current Task and starts the program from the home page, the picture is displayed, which will disturb the user, therefore, set FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET in Activity B. When the Task is returned, Activity B is destroyed. The Task stack status is, therefore, the interaction with the user is Activity A rather than the image that troubles the user.
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
If set, the new Activity will not be saved in the list of recently started activities.
FLAG_ACTIVITY_FORWARD_RESULT
If you set and use the Intent to start a new Activity from an existing Activity, the Activity that replies to the target will be uploaded to the new Activity. In this way, the new Activity can call setResult (int), and the result value will be sent to the Activity that is used as the reply target.
This Flag has the same effect as startActivityForResult (intent, requestCode.
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
This identifier is generally not set by the program code, but set by the system when the Activity is started in the History (long press the home key ).
FLAG_ACTIVITY_MULTIPLE_TASK
Do not use this flag unless you have implemented the application launcher yourself. It can be used with FLAG_ACTIVITY_NEW_TASK to disable sending existing tasks to the foreground. When set, the new Task will always start to process Intent, regardless of whether a Task can process the same thing. Because the default system does not contain the graphical Task management function, you should not use this flag unless you provide users with a way to return started tasks. If the FLAG_ACTIVITY_NEW_TASK flag is not set, this flag is ignored.
FLAG_ACTIVITY_NEW_TASK
Start the Activity in a new stack. If the Activity you want to start is already running in a stack, the stack will remember its recent status and be placed on the foreground, the onNewIntent () in this Activity receives this new intent, which has the same behavior as the singleTask in launchMode.
When this ID is set, the system first checks whether there is a job stack with the same affinity as the started Activity (that is, taskAffinity, note that the Activity in the same application has the same affinity ), if yes, just move the entire stack to the foreground and keep its status unchanged. That is, the Activity sequence in the stack remains unchanged. If not, create a new stack to store the started Activity.
1. premise: Activity A, B, and C are all in "FlagTest application ".
Scenario: Activity A starts and opens the Task stack. The status is A. Then Activity A starts Activity B and Activity B starts Activity C. Activity C sets FLAG_ACTIVITY_NEW_TASK, by default, the taskAffinity of the same application is the same, and the Task stack status is ABC.
2. premise: Activity A is in the "FlagTest application", Activity B and C are in the "FlagTest Application 1 ".
Scenario Demonstration: Activity A starts to open up the Task stack (TaskA status: A), Activity A starts Activity B, and Activity B sets FLAG_ACTIVITY_NEW_TASK, because it is A different application, its taskAffinity is different, it opens up the Task stack (TaskB status: B), Activity B starts Activity C, at this time Task kb status: BC. When Activity B is restarted from Activity A, because Activity B is already in task kb, task KB is returned to the foreground, and the Activity sequence in task kb remains unchanged, that is, Activity C is displayed to the user.
FLAG_ACTIVITY_NO_ANIMATION
The Activity started by the intent that sets this identifier will not have a transition Animation
FLAG_ACTIVITY_NO_HISTORY
The Activity started by the intent with this identifier is not put into the Task.
Scenario Demonstration: Activity A launches TaskA. Status: A, Activity A starts Activity B, and Activity B sets FLAG_ACTIVITY_NO_HISTORY. After Activity B starts Activity C, the status of TaskA is AC, because Activity B is not put into the Task.
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
This identifier is generally used with FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET. For details, see FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET above.
FLAG_ACTIVITY_SINGLE_TOP
This ID indicates that at the top of the stack, one Activity can only have one instance.
Scenario Demonstration: Activity A launches TaskA and starts Activity B in Activity A. Activity B sets the FLAG_ACTIVITY_SINGLE_TOP identifier. At this time, the status of TaskA is AB, when intnet requests Activity B, it does not create an instance of Activity B, but directly uses Activity B in the original stack. At this time, the status of TaskA is AB. If it is the default method, then, an Activity B is instantiated, And the TaskA status is ABB.
These Intent flags are commonly used flags in development. You can set these flags to easily manage the activities in the project and adapt them to the Activity jump logic in the project, of course, to manage all the activities in the project more effectively, you should implement the management functions similar to tasks on your own.
Ii. LaunchMode
This part has a detailed introduction to http://developer.android.com/guide/topics/manifest/activity-element.html in the official API guides
There are four startup modes for the Activity ,"standard","singleTop","singleTask","singleInstance", The default start mode is standard. The four modes are divided into four. standard and singleTop are one type. The Activity can be instantiated multiple times. These instances can belong to any stack, singleTask and singleInstance are the other one. They can only open one stack and always exist in the low stack.
| Usage |
Startup Mode |
Can I instantiate it multiple times? |
Note |
| Most Activity start Modes |
Standard |
Yes |
By default, an instance is always created. |
| SingleTop |
Conditional |
Do not create an instance with Activity modification at the top of the stack. Others are the same as standard. |
| Special startup (not recommended) |
SingleTask |
No |
As the root Activity of a New stack, it can only be instantiated once |
| SingleInstance |
No |
Similar to singleTask, the stack of an Activity with singleInstance can only store this Activity. |
3. <activity> attribute in Manifest
This section is detailed in the official developer documentation at http://developer.android.com/guide/topics/manifest/activity-element.html;
Intent-related attributes: 1, taskAffinity; 2, alwaysRetainTaskState; 3, clearTaskOnLaunch; 4, finishOnTaskLaunch
1. taskAffinity attributes:
(1) The taskAffinity attribute should be used in combination with the FLAG_ACTIVITY_NEW_TASK flag and allowTaskReparenting attribute.
(2) combined with the FLAG_ACTIVITY_NEW_TASK flag:
A. Prefix: Activity A and B are in the same application. Activity A and Activity B set different taskAffinity attributes.
Scenario: Activity A starts the uncommon Task Stack, named TaskA (TaskA stack status: A), starts Activity B in Activity A, and starts Activity B
Set the FLAG_ACTIVITY_NEW_TASK flag in Intent. In this case, the system will open a new Task stack, TaskB (TaskB stack status: B ).
B. Prerequisites: Activity A is in the "FlagTest application" and Activity C is in the "FlagTest1 application". Activity A and ActivityC set the same taskAffinity attribute.
Scenario Demonstration: Activity A starts an uncommon Task Stack named TaskA (TaskA stack status: A), starts Activity C in Activity A, and sets the FLAG_ACTIVITY_NEW_TASK flag in Intent of Activity C, at this time, Activity C will press the TaskA stack that is the same as Activity A stack (TaskA stack status: AC ).
(3) attributes with allowTaskReparenting:
Scenario: In the "FlagTest application", Activity A has A default relationship with other activities in the "FlagTest application" (The taskAffinity attribute is not set ), and the allowTaskReparenting attribute is set to true. There is now Activity A in the "FlagTest1 application" started "FlagTest application". At this time, Activity A and Activity in the "FlagTest1 application" are in the same Task, after the Task Stack is named TaskA, "FlagTest application" is started and Activity A is opened again. Then Activity A is transferred from the TaskA stack to the stack where the "FlagTest application" is located, that is, Activity A can be transferred back and forth in multiple stacks.
2. alwaysRetainTaskState attributes:
If the value of this attribute is set to true for the Root Activity in the Task stack, the status of the Activity in the Task stack is retained no matter what happens.
3. Attributes of clearTaskOnLaunch:
If the value of this attribute is set to true for the Root Activity in the Task stack, as soon as you exit the Task stack, the system will immediately clear all the activities except the Root Activity.
4. finishOnTaskLaunch attributes:
If a finishOnTaskLaunch attribute is set for an Activity, as soon as you exit the Task stack, the system will immediately clear the Activity, regardless of its location in the stack.