Stack Management of activity

Source: Internet
Author: User

Stack Management of activity

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 tags, 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 tag 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 tag. 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.

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + encrypt/decrypt + CiAgICDSu7 + encrypt/decrypt + 7j2yfrD/KGjyLu2 + NPQ0rvQqcfpv/decrypt + increment/decrypt + m/9 s /messages + messages/z1tCw/messages + HI57rOzPjXqqO/messages + hozwvcD4KPHA + CjIu1Nq/messages + HI57rOzPjXqqO/messages + messages + G/strong + weight/gzay1xMfXyvS52M + weight/weight + sPy6rMHL0ru49tLUyc + weight/weight + m8/weight + weight/Weight = "singleTop" is equivalent to the ACITIVITY start
"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 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 tag to "true.

Refer:

TaskAffinity

SingalTask and singalInstace

Activiy job Stack


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.