[Translation] Android Application basics> job stacks and kinship (activities and tasks)

Source: Internet
Author: User

In Android, an activity component can activate another activity component (which may belong to another application ).Program).

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.In general, 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:

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_reset_task_if_needed

Flag_activity_single_top

In <Activity>Tag, the main attributes are:

Taskaffinity

Launchmode

Allowtaskreparenting

Cleartaskonlaunch

Alwaysretaintaskstate

Finishontasklaunch

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

1. kinship and new tasks

By default, activity components in an application are kinship with each other-that is, they belong to the same task stack. However, you can set<Activity>The taskaffinity attribute of the tag to set kinship for this activity component. 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. Kinship will play a role in two situations:

1) the intent object that is responsible for activating the activity component containsFlag_activity_new_taskFlag.

2)AllowtaskreparentingProperty is set to "true ".

AboutFlag_activity_new_taskFlag quantity

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 containsFlag_activity_new_taskThe 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.

AboutAllowtaskreparentingAttribute

If an activity component'sAllowtaskreparentingIf it is set to "true", when the job stack with the same kinship with this 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.

2. Startup Mode

<Activity>The launchmode attribute of a tag can be set to four different modes:

"Standard"(Default mode)

"Singletop"

"Singletask"

"Singleinstance"

The differences between these modes are as follows:

1) the stack of the task to which the activity is activated.

ForStandardAndSingletop"Mode, the newly activated activity will be placed in the same job stack as the previous activity-unless, as described earlier, the intent object containsFlag_activity_new_taskFlag.

But"SingletaskAndSingleinstance"Mode indicates that the newly activated activity will not be placed in an existing Task Stack. It will re-open a task stack and act as the root activity of the new task stack.

2) whether multiple instances of this activity type can exist.

ForStandardAndSingletop"Mode, you can have multiple instances, and these instances can belong to different task stacks, each task stack can also contain multiple instances of this activity type.

But"SingletaskAndSingleinstance"Mode indicates that at most one instance of this activity type can exist. Because the first point must be the root activity, this means that at the same time, there will never be more than one task stack started by the activity on the mobile phone.

3) whether the task stack containing this activity can contain other activities.

"Singleinstance"Mode indicates that the task stack containing this activity cannot contain other activities. If this activity starts another activity component, no matter what the Startup Mode of the activity component is or whether the intent object containsFlag_activity_new_taskMark, it will be put into another job stack. In other aspects,Singleinstance"Mode and"Singletask"Mode is the same.

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.

ForSingletop"Mode, only when the instance of this activity is currently at the top of the stack of the task stack, it will be reused to handle the newly arrived intent object. Otherwise,Standard.

As the second point says,"SingletaskAndSingleinstance"Mode indicates that only one instance is allowed. Therefore, this unique instance must process all newly arrived intent objects. Because"Singleinstance"The mode of activity instance is always located at the top of the stack of the task stack, so this is normal. However, for acitvity in singletask mode, other activity components may exist on it, so its position is not the top of the stack. In this case, the intent object will be discarded. (Although discarded, this intent object will switch the job stack to the foreground)

If a new intent object is processed by an existing activity componentOnnewintent (Android. content. Intent)The 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.

3. Clear the Task Stack

If a task stack is kept in the background for a long time, 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 default mode is used.<Activity>LabelSomeAttribute to change:

1)AlwaysretaintaskstateAttribute

IfAlwaysretaintaskstateIf the attribute is set to "true", the system will not clean up the task stack even if a task stack is kept in the background for a long time.

2)CleartaskonlaunchAttribute

IfCleartaskonlaunchIf the attribute is set to "true", only the job stack is switched to the background, the system will clear all other activities except the root activity in the job stack. That is, SumAlwaysretaintaskstateThe opposite is true.

3)FinishontasklaunchAttribute

The behavior of this attribute is similarCleartaskonlaunchBut 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 containsFlag_activity_clear_topAnd an instance of the activity type used to process this intent object already exists in the target Task Stack. Then, 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 matching activity type is"Standard", The activity-type instance 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.

Flag_activity_clear_topThis sign often matchesFlag_activity_new_taskMark is used in combination. This means to locate an existing activity-type instance in another task stack and place the instance at the top of the stack.

4. Start the Task Stack

Set the intent-filter action of an activity type to"Android. Intent. Action. Main", Set category to"Android. Intent. Category. LauncherThis activity instance can be called the entry of a job 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, enable the startup mode ("SingletaskAndSingleinstanceMode.Android. Intent. Action. Main"Action and"Android. Intent. Category. Launcher"CATEGORY intent-filter activity type.

Similar restrictions are also reflected inFlag_activity_new_taskFlag. 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 alwaysFlag_activity_new_taskFlag is placed in the intent object, and the intent object is passed inStartactivity ()Method.

In some cases, if you do not want the user to return to an activity, you can set<Activity>"Finishontasklaunch"Attribute is" true.

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.