Stack management and activity management

Source: Internet
Author: User

Stack management and activity management

In android, an activity component can activate another activity component: the activity of this program and other programs.
If the new activated activity component belongs to another application, the activity component runs in the Process of that application, but from the user's perspective, it seems to belong to this application. Android implements this function by placing the previous activity component and the newly activated activity component into the same task stack. From the user's perspective, a job stack represents "one application ". It is actually a stack with a group of activity components arranged. The activity (root activity) at the bottom of the stack is the activity component that enables the task stack. Generally, it is the main interface of the application. The activity component at the top of the stack represents the currently activated activity component (the activity that can receive user behavior ).
The task stack contains the objects of the activity component, and the task stack can contain multiple instance objects of the activity component type. The activity components in the task stack cannot be reordered, but can only be pushed to the stack or the stack.
A Task Stack is neither a type nor an element, but an organizational form of a group of activity components. Therefore, there is no way to set the parameters of the task stack independently without affecting the activity components in the task stack. The parameter of the root activity is not only the parameter of the entire task stack, but also affects all activity components in the task stack.



When an application is switched between the frontend and backend, a task stack of the application is switched between the frontend and backend.
The behavior you just described is the default behavior of activity and task stack, but there are also ways to modify it in many ways:

1. Use taskAffinity and allowTaskReparenting

Method 1: set some tags in the sent request (the Intent object.
Method 2: In the manifest file, set some attributes for the activity component that receives the request (that is, the Intent object.
Therefore, it can be controlled by both the requester and receiver.
Main Intent objects are marked as follows:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
In the <activity> tag, the main attributes are:
TaskAffinity android: taskAffinity = "com. cardroid. sdhc "indicates the kinship between two applications. If ActivityL1 in one application and ActivityR1 in Application2 in another application, the ActivityL1 allowTaskReparenting = true allows the task stack to be dynamically loaded.
Then, after ActivityL1 is displayed, click the HOME keyboard. After ActivityR1 is started from another application, the ActivityL1 that clicked HOME is displayed at the front-end, because ActivityL1 is loaded for the dynamic task stack, and because of the existence of taskAffinity, therefore, it is added to the ActivityR1 Task Stack and displayed on the foreground.


The following describes the functions and usage of Intent labels and <activity> label attributes.

AllowTaskReparenting

LaunchMode
ClearTaskOnLaunch
AlwaysRetainTaskState
FinishOnTaskLaunch

2. kinship and new tasks by default, the activity components in an application are kinship with each other-that is, they belong to the same task stack. However, we can set kinship for this activity component by setting the taskAffinity attribute of a <activity> label. Activity components defined in different applications can share the same kinship, or different relatives can be used for activity components defined in the same application. Each Activity has the taskAffinity attribute, which specifies the Task it wants to enter.

If an Activity does not explicitly specify the taskAffinity of the Activity, its attribute is equal to the taskAffinity specified by the Application. If the Application does not, the value of taskAffinity is equal to the package name. Task also has its own affinity attribute. Its value is equal to the taskAffinity value of its root Activity.
At the beginning, the created Activity will be in the Task where it is created, and most of the activities have passed its entire life here. However, in some cases, the created Activity will be allocated to other tasks, and some may even be transferred after being originally in a Task. Kinship will play a role in two situations:
1) the Intent object that is responsible for activating the activity component contains the FLAG_ACTIVITY_NEW_TASK flag.
2) The allowTaskReparenting attribute of the activated activity component is set to "true ".

Problem:

1. In the same program, different activities have the same (default) or different taskAffinity attributes. How do I jump to the default FLAG_ACTIVITY_NEW_TASK flag? There are 4 combinations.

2. When different programs start an Activity, different activities have the same or different (default) taskAffinity attributes. How do I jump to the default FLAG_ACTIVITY_NEW_TASK flag? There are 4 combinations.

FLAG_ACTIVITY_NEW_TASK flags
By default, an activated new activity and the activity component that is responsible for activating it exist in the same task stack. However, if the Intent object to be activated contains the FLAG_ACTIVITY_NEW_TASK flag, the system will find a new job stack for the newly activated activity. At this time, if a task stack with the same kinship already exists, the system will directly put the activated new activity into the task stack; otherwise, the system starts a new task stack.

About allowTaskReparenting attributes
If the allowTaskReparenting of an activity component is set to "true", when the job stack with the same kinship with the activity is switched to the foreground, this activity moves from the current task stack to the task stack with the same kinship.
If a .apk file contains more than one "application", you may need to specify different kinship relationships for those activity components.

3. The launchMode attribute of the startup mode <activity> label can be set to four different modes:
"Standard" (default mode)
"SingleTop"
Android: launchMode = "singleTop" is equivalent to starting from this ACITIVITY every time
"SingleTask"
Android: launchMode = "singleTop" and singleTop are similar. different from each other
"SingleInstance"
The differences between these modes are as follows:
1) the stack of the task to which the activity is activated.
For "standard" and "singleTop" modes, the newly activated activity will be placed in the same job stack as the previous activity-unless, as described earlier, the Intent object contains the FLAG_ACTIVITY_NEW_TASK flag.
However, the "singleTask" and "singleInstance" modes indicate that the newly activated activity will not be placed in an existing job stack, and it will re-open a job stack, and act as the root activity of the new job stack.


2) whether multiple instances of this activity type can exist.
For the "standard" and "singleTop" modes, there can be multiple instances, and these instances can belong to different task stacks, each Task stack Task can also contain multiple instances of this activity type.
However, the "singleTask" and "singleInstance" modes indicate that at most one instance of this activity type can exist. This must be the limitation of the root activity. A task can only be instantiated once. So this means that at the same time, there will never be more than one task stack started by this activity on the mobile phone.

3) whether the task stack containing this activity can contain other activities.
The "singleInstance" mode indicates that the task stack containing the activity cannot contain other activities. If this activity starts another activity component, it will be put into another task stack regardless of the Startup Mode of the activity component or whether the Intent object contains the FLAG_ACTIVITY_NEW_TASK flag. In other aspects, the "singleInstance" mode is the same as the "singleTask" mode.
The other three startup modes allow the Task Stack containing the activity to contain other activities.

4) Whether a new instance of the class will be launched to handle a new intent.
For the default "standard" mode, every time an Intent object is returned, a new instance of this activity type will be created. Each activity instance processes an intent.
For the "singleTop" mode, only when the activity instance is currently at the top of the stack of the task stack, it will be reused to handle the newly arrived intent object. Otherwise, the behavior is the same as that in the "standard" mode.
As mentioned in the second point, the "singleTask" and "singleInstance" modes indicate that only one instance is allowed. Therefore, this unique instance must process all newly arrived intent objects. Because the activity instance in singleInstance mode is always at the top of the stack of the task stack, it is normal to do so.

However, for acitvity in singleTask mode, if the Intent object sent to the Activity is not at the top of the stack, the Intent object will be discarded, however, the interface will still switch to the current Activity. (Although discarded, this intent object will switch the job stack to the foreground)
If a new intent object is processed by an existing activity componentThe NewIntent (android. content. Intent) method is called by the system.

NOTE: If an activity instance is created to process a new intent object, the user presses the "BACK" key to return to the previous activity. However, if the newly arrived intent object is processed by an existing activity component, if you press the "BACK" key, it will not be rolled BACK to the status before processing the new intent object.

4. clear the job stack. If a job stack is kept in the background for a long time, then the system will clear all the other activities except the root activity in the task stack. After that, when you switch the job stack to the foreground, only the root activity is displayed.
The above is the default mode, which can be changed through some attributes of the <activity> tag:
1) alwaysRetainTaskState attribute
If you set the alwaysRetainTaskState attribute of the root activity to "true", even if a job stack is kept in the background for a long time, the system will not clean up the task stack.
2) clearTaskOnLaunch attributes
If you set the clearTaskOnLaunch attribute of the root activity to "true", only the task stack is switched to the background, then the system will clear all the other activities except the root activity in the task stack. That is, the behavior of alwaysRetainTaskState is completely the opposite.
3) finishOnTaskLaunch attributes
The behavior of this attribute is similar to clearTaskOnLaunch, but this attribute acts on a single activity object rather than the entire task stack. When the job stack is switched to the background, this attribute allows the job stack to clear any activity objects including the root activity.

There is another way to remove the activity object from the task stack. If the Intent object contains the FLAG_ACTIVITY_CLEAR_TOP flag and an instance of the activity type used to process the Intent object already exists in the target Task Stack, all activity instances on this instance in the task stack will be removed. The instance used to process the activity type of the Intent object is located at the top of the stack of the task stack and used to process the Intent object. If the Startup Mode of the combined activity type is "standard", the instance of the activity type that already exists in the task stack will also be removed, in addition, a new instance of this type of activity is created and the stack is pressed to process this Intent object.

The FLAG_ACTIVITY_CLEAR_TOP flag is often used in combination with the FLAG_ACTIVITY_NEW_TASK flag. In this way, it is used to locate an existing active activity instance in another task stack, and place the instance at the top of the stack.

5. Start the task stack by setting the intent-filter action of an activity type to "android. intent. act" Ion. MAIN "and the category is set to" android. intent. category. LAUNCHER ". This activity instance can be called the portal of a task stack. Charts and names of activity types with intent-filter of this type are also displayed in application launcher.

The second capability is very important: You must be able to switch a job stack to the background or switch it to the foreground at any time. For this reason, the startup mode ("singleTask" and "singleInstance" modes) of the newly opened task stack of the activity should only be used to own "android. intent. act" Ion. MAIN "action and the intent-filter activity type of the" android. intent. category. LAUNCHER "category.
Similar restrictions are also reflected in the FLAG_ACTIVITY_NEW_TASK flag. If this flag enables an activity to start a new job stack, and the user clicks the "HOME" key to switch it to the background, there must be some way for the user to switch the job stack to the foreground. Some instances (such as the notification manager) always enable an activity in the external task stack, rather than their own task stack, so they always put the FLAG_ACTIVITY_NEW_TASK flag into the Intent object, and pass the Intent object to the startActivity () method.
In some cases, if you do not want the user to return to an activity, you can set the "finishOnTaskLaunch" attribute of the <activity> label to "true.

Refer:

TaskAffinity

SingalTask and singalInstace

Activiy job Stack



How to use activity stack management to implement forward and backward activity

In intent, add Intent. FLAG_ACTIVITY_REORDER_TO_FRONT to indicate that if the activity has been started, no new activity will be generated. You just need to add the activity instance to the top of the stack. The Code is as follows: Java code Intent intent = new Intent (ReorderFour. this, ReorderTwo. class); intent. addFlags (Intent. FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity (intent );
 
How to independently manage the two groups of fragment back stacks in an activity

There is a stupid way. Set a currentViewController to determine which one is currently
 

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.