Android task and Activity Analysis

Source: Internet
Author: User
Tags home screen

 

Android: allowtaskreparenting
Indicates whether the activity can be moved from the started task to a task with affinity (when the task enters the foreground) -- "true", indicating that the activity can be moved, "false ", indicates that it must stay in the task at startup.
If this feature is not set, the value of the allowtaskreparenting attribute set to the <Application> element is applied to the activity. The default value is "false ".
Generally, when an activity is started, it is associated with the task that starts it and consumes its entire lifecycle. When the current task is no longer displayed, you can use this feature to force the activity to move to a task with affinity. A typical usage is to move the activity of an application to the main task of another application.
For example, if e-mail contains a webpage link, clicking it will start an activity to display this page. This activity is defined by the browser application, but now it is part of the E-mail task. If it re-hosts to the browser task, when the browser enters the foreground next time, it can be seen, and when the e-mail task enters the foreground again, you can't see it.
The affinity of actvity is defined by the taskaffinity feature. The affinity of a task is determined by reading the affinity of the root activity. Therefore, according to the definition, the root activity is always located in the task with the same affinity. Because the activity in the startup mode of "singletask" and "singleinstance" can only be located at the bottom of the task, re-hosting can only be limited to "standard" and "singletop" modes.

Android: alwaysretaintaskstate
Used to mark whether the status of the task where the activity is located is always maintained by the system -- "true", indicating always; "false ", it indicates that the system is allowed to restore the task to its initialization State under certain circumstances. The default value is "false ". This feature is only valid for the root activity of the task. For other activities, ignore it.
In general, in a specific situation, for example, when you reselect the task from the main screen, the system will clean up the task (delete all activivity located above the root activity from the stack ). In typical cases, this is also done when the user does not access this task for a period of time, for example, 30 minutes.
However, when this feature is set to "true", the user can always return to the latest status of the task, no matter how they start it. This is very useful. For example, for browser applications, there are many statuses (such as multiple open tabs), and users do not want to lose these statuses.

Android: cleartaskonlaunch
Used to mark whether to clear all the activities from the task, except for the root activity (whenever the main screen is restarted) -- "true", indicating that it is always cleared to its root activity, "false" indicates no. The default value is "false ". This feature is only meaningful to the activity (root activity) that starts a new task; it is ignored for other activities in the task.
When the value is "true", every time the user restarts the task, it will enter its root activity, no matter what the task is doing, whether the user uses back or home to exit. When the value is "false", the activity of the task may be cleared in some cases (refer to the alwaysretaintaskstate feature), but not always.
Assume that someone starts activity P from the main screen and migrates it to activity Q from there. Next, press home and return activity p. Generally, you may see activity Q, because it is the final work content of the P task. However, if p sets this feature to "true", when the user presses home and causes this task to enter the foreground again, all the activities on it (Q here) will be cleared. Therefore, when this task is returned, the user can only see P.
If both this feature and allowtaskreparenting are set to "true", the re-host activity will be moved to the task that shares affinity; the remaining activity will be discarded, as described above.

Android: finishontasklaunch
It is used to mark whether the existing activity instance needs to be closed (ended) -- "true" when the user starts the task again (this task is selected on the main screen), indicating that the task should be closed, "false" indicates not to close. The default value is "false ".
If both this feature and allowtaskreparenting are set to "true", this feature wins. The affinity of the activity is ignored. This activity will not be re-hosted, but will be destroyed.

Android: launchmode
Used to indicate how the activity is started. There are four modes, which work with the activity flags (flag_activity _ * variable) in the intent object to determine how the activity starts to process the intent. They are:

"Standard"
"Singletop"
"Singletask"
"Singleinstance"

The default mode is "standard ".

These modes can be divided into two groups: "standard" and "singletop", "singletask" and "singleinstance. Activity with "standard" and "singletop" Startup modes can be instantiated many times. These instances can belong to any task and can be located anywhere in the activity stack. Typically, they enter the task that calls startactivity () (unless the intent object contains the flag_activity_new_task flag, a different task will be selected in this case-refer to the taskaffinity feature ).
On the contrary, "singletask" and "singleinstance" can start only one task. They are always at the bottom of the activity stack. Even a device can have only one activity instance at a time-only one such task.
The "standard" and "singletop" modes are only different in one case: each time a new intent starts the "standard" activity, A new instance is created to respond to the intent. Each instance processes an intent. Similarly, a "singletop" activity instance may be created to handle a new intent. However, if the target task already has an instance at the top of the stack, the instance will receive the new intent (onnewintent ()); no new instance is created. In other cases -- for example, if an existing "singletop" activity instance is in the target task, but is not at the top of the stack, or is at the top of a stack, but not in the target task-new instances will be created and pushed into the stack.
The "singletask" and "singleinstance" modes are only different in one case: the "singletask" activity allows other activities to become part of its task. It is located at the bottom of the activity stack. Other activities (which must be "standard" and "singletop") can be started and added to the same task. "Singleinstance" activity, in other words, does not allow other activities to become part of its task. It is the only activity in a task. If it starts other activities, the activity will be placed in another task -- as if intent contains the flag_activity_new_task flag.

Android: nohistory
Used to mark whether the activity is cleared and ended from the activity stack when the user leaves the activity and is no longer visible on the screen (call the finish () method) -- "true ", it indicates that it should be disabled, and "false" indicates that it is not required. The default value is "false ".
The "true" value means that the activity does not leave any trace of history. Because it is not retained in the task of the activity stack, you cannot return it.

Android: taskaffinity
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.

Flag_activity_brought_to_front
This flag is not set by the program code. For example, the system helps you set the singletask mode in launchmode.

Flag_activity_clear_top
If it is set and the activity is already running in the current task, it is no longer to restart an instance of the activity, but all the activities above the activity will be closed, the intent is then delivered to the old activity (currently at the top) as a new intent.
For example, assume that a task contains the activity A, B, C, and D. If startactivity () is used in the call, and contains an intent pointing to Activity B, both c and d end, and B receives the intent. Therefore, the current stack status is a and B.
In the preceding example, the running Activity B can receive the new intent in onnewintent (), disable it, and restart it to receive the intent. If its startup mode is declared as "multiple" (default), and you have not set the flag_activity_single_top flag in this intent, it will be disabled and re-created; for other startup modes, or set the flag_activity_single_top flag in the intent to ship the intent to the onnewintent () of the current instance.
This startup mode can also be used in combination with flag_activity_new_task: used to start the root activity in a task. It will bring any running instance in that task to the foreground, and then clear it until the root activity. This is very useful, for example, when an activity is started from notification manager.

Flag_activity_clear_when_task_reset
If this parameter is set, a restore point is set in the activity stack of the task. When the task is restored, the activity needs to be cleared. That is to say, when the next task is marked to enter the foreground with flag_activity_reset_task_if_needed (the typical operation is that the user restarts the activity on the main screen), the activity and the above will be closed, so that users can no longer return to them, but can return to the previous activity.
This is useful when your program has a split point. For example, an e-mail application may have an operation to view an attachment. You need to start the image browsing activity to display the attachment. This activity should be part of the E-mail application task because it is an operation triggered by the user in this task. However, when the user leaves this task and selects the email app from the main screen, we may want to go back to the viewing session, but not view the Image Attachment, because this is confusing. By setting this flag when starting Image Browsing, browsing and other started activities will be cleared the next time the user returns to the mail program.

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.

Flag_activity_launched_from_history
This flag is generally not set by the application code. if the activity is started from the history (usually press the Home Key), the system will help you set it.

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
If this parameter is set, the activity becomes the start of a new task in the history stack. A task (from the activity that starts it to the activity in the next task) defines the activity atomic group that users can migrate. Tasks can be moved to the foreground and background. All activities in a specific task are always in the same order.
This flag is generally used to present the "Start" type of behavior: they provide a series of things that can be done independently, and have nothing to do with starting their activity.
With this flag, if the task of the activity being started is already running, the new activity will not be started; instead, the current task will be simply moved to the foreground. Refer to the flag_activity_multiple_task flag to disable this line.
This flag cannot be used by the caller for the results of the activity request that has been started.

Flag_activity_no_animation
If it is set in intent and passed to context. startactivity (), this flag will prevent the system from applying the acitivity migration animation when entering the next activity. This does not mean that the animation will never run-if this flag is not specified before another activity is started, the animation will be applied. This flag can be used to execute a series of operations, and animation is seen as the driver of a higher level of events.

Flag_activity_no_history
If this parameter is set, the new activity will not be retained in the history stack. The activity is closed as soon as the user leaves it. You can also set the nohistory feature.

Flag_activity_no_user_action
If this parameter is set, when a newly started activity enters the foreground, this flag will block onuserleavehint () from the previous activity callback before the activity is paused ().
Typically, an activity can depend on this callback to indicate that the activity caused by an explicit user action is removed from the background. This callback marks an appropriate vertex in the lifecycle of the activity and closes some notifications.
If an activity is started through a non-user-driven event, such as a power-on or alarm clock, this flag should also be passed to context. startactivity to ensure that the paused activity does not think that the user knows its notification.

Flag_activity_previus_is_top
If set and this intent is being used to launch a new activity from an existing one, the current activity will not be counted as the top activity for deciding whether the new intent shoshould be delivered to the top instead of starting a new one. the
Previous activity will be used as the top, with the assumption being that the current activity will finish itself immediately.

Flag_activity_reorder_to_front
If it is set in intent and passed to context. startactivity (), this flag will cause the running activity to move to the top of the historical stack.
For example, assume that a task is composed of four activities: A, B, C, and D. If startactivity () is used to start Activity B by calling "D", B will move to the top of the history stack, and the current order will be a, c, d, and B. If the flag_activity_clear_top flag is also set, this flag will be ignored.

Flag_activity_reset_task_if_needed

If set, and this activity is either being started in a new task or bringing to the top an existing task, then it will be launched as the front door of the task. this will result in the application of any affinities needed to have that task in the proper state
(Either moving activities to or from it), or simply resetting that task to its initial state if needed.

Flag_activity_single_top
If it is set, when this activity is running at the top of the history stack, a new one is no longer started.

Activity and task

 

As mentioned earlier, one activity can start another activity, even the activity defined in different applications. For example, suppose you want users to display street views in some places. Here, an activity can do this. Therefore, all you need to do is to add necessary information to the intent object and pass it to startactivity (). Map Browsing will display your map. When you press the back key, your activity will appear on the screen again.

 

For users, it seems that map browsing is the same as your activity and belongs to the same application, even if it is defined in other applications, and run in the Process of the application. Android stores these two activities in the same task to demonstrate this user experience. Simply put, a task is an "application" for user experience ". It combines related activities and manages them in stack mode. In stack, the root activity starts a task, which is a typical activity selected by the user in the application startup bar. The activity at the top of the stack is currently running-the activity that can focus on the user. When one activity starts another activity, the new activity enters the stack; it becomes a running activity. The previous activity is still in the stack. When the user presses the back key, the current activity exits from the stack, and the previous one becomes a running activity.

 

Stack contains objects. Therefore, if a task contains multiple instances of the same activity-multiple map views, for example, a stack has an independent portal for each instance. The activity in the stack is not adjusted, but enters and exits.

 

A task is a group of activities, not a class or an element defined in manifest. Therefore, there is no way to set the attribute value of the activity independent of the task. Set the Task value as a whole in the root activity. For example, the next chapter will discuss the "affinity" of the task. The value is read from the root activity of the task.

 

All the activities in the task are moved together as a unit. The entire task (the entire activity stack) can enter the foreground or return to the background. For example, assume that four or three of the stack in the current task are under the current activity. Press the Home Key to enter the application startup bar and select a new application (in fact, a new task ). The current task is returned to the background, and the root activity in the new task is displayed. Then, after a period of time, the user returns to the home screen and selects the previous application (the previous Task) again ). The task with four activities will enter the foreground. When you press the back key, the screen does not show the activity you just left (the root activity of the previous Task ). Instead, the top activity in the stack is removed, and the previous activity in the same task is displayed.

 

The Act described above is the default act of activity and task. But there is a way to completely change it. The association between tasks and an activity behavior in a task is subject to the feature value interaction control of the Flag Set in the intent object of the started activity and the activity <activity> element in the manifest file. Both the caller and the responder have the right to decide how to happen.

 

The core intent flags include:

Flag_activity_new_task

Flag_activity_clear_top

Flag_activity_reset_task_if_needed

Flag_activity_single_top

 

Core <activity> features include:

Taskaffinity

Launchmode

Allowtaskreparenting

Cleartaskonlaunch

Alwaysretaintaskstate

Finishontasklaunch

 

The following sections describe how to use flags and features, how they affect each other, and how to use them.

 

Affinity and new task

By default, all the activities in an application have affinity -- that is, all the activities in the same task have a setting. However, each activity can set a separate value on the taskaffinity feature of the <activity> element. Activity defined in different applications can share the same affinity, or define activity defined in the same application to set different affinity. Affinity works in two environments: The intent object contains the flag_activity_new_task flag, and the allowtaskreparenting feature of the activity is set to "true ".

Flag_activity_new_task:

As described earlier, an activity is generally started by calling startactivity () and added to the task. It enters the same task as the caller. However, if the intent object passed to startactivity () contains flag_activity_new_task, the system searches for a new task to accommodate the new activity. Generally, as shown in the flag name, it is a new task. However, it is not necessary. If a task already exists with the same affinity as the new activity, the activity will be added to the task. If not, start a new task.

Allowtaskreparenting:

If the allowtaskreparenting attribute of an activity is set to "true", it can be moved from the started task to the task with the same affinity (when the task enters the foreground ). For example, a tourism program defines an activity that can report the weather conditions of a selected city. Like other activities of the same application, it has the same affinity (default affinity) and allows re-host. One of your activities starts the weather forecast, so it is initialized to the same task as your activity. However, when the travel application enters the foreground next time, the activity of the weather forecast will be rearranged and displayed in that task.

 

If a .apk file contains multiple "Applications" from the user's perspective, you may want to set different affinity for the associated activity.

 

Launch Mode

 

Here, four different startup modes can be set to the launchmode feature of the <activity> element:

Standard (default mode)

Singletop

Singletask

Singleinstance

 

These modes have the following four differences:

L which task will hold the activity that responds to the intent. For "standard" and "singletop", it is the task that generates intent (and calls startactivity () unless the intent object contains flag_activity_new_task. In that case, different tasks will be selected, as described in "affinity and new task. In comparison, "singletask" and "singleinstance" indicate that the activity is always the root of a task. They define one task; they are not added to another task.

L whether there are multiple activity instances. "Standard" and "singletop" can be instantiated multiple times. They can belong to multiple tasks. A specific task can have multiple instances of the same activity. In comparison, "singletask" and "singleinstance" can only have one instance. Because these activities can only be located at the bottom of a task, this limit means that multiple instances of such tasks will not appear at a certain time of the device.

L whether other activities can be owned in the same task. The "singleinstance" activity remains single and is the only activity in its tasks. If it starts another activity, the activity will be put into different tasks, regardless of its Startup Mode-as if flag_activity_new_task is in intent. For other aspects, "singleinstance" is equivalent to "singletask ". The other three modes allow multiple activities to be added to the task. The "singletask" activity is always at the bottom of the task, but it can start other activities and put them into its task. "Standard" and "singletop" activity can appear anywhere on the stack.

L whether a new instance is started to handle the new intent. For the default "standard", a new instance is created to respond to the new intent. Each instance processes an intent. For "singletop", if it is at the top of the target task, existing instances can be reused to process the new intent. If it is not at the top, it cannot be reused. Instead, the new instance will be created to respond to the new intent and enter the stack.

For example, assume that the activity stack of a task contains the Root Activity A and other activity B, C, D, and D at the top. Therefore, the stack is a-B-c-d. There is an intent that starts Activity D. If D has the default "standard" startup mode, a new instance will be started and the stack will become a-B-c-d. However, if the Startup Mode of D is "singletop", the existing instance will process the new intent (because it is at the top of the stack ), and the stack is still a-B-c-d.

In other words, if the arrival intent is directed to the B type, then, b-type instances will be created and started, regardless of whether B's mode is "standard" or "singletop" (because B is not at the top of the stack). Therefore, stack will be a-B-c-d-B.

As mentioned earlier, there will be no "singletask" or "singleinstance" activity on the device that exceeds one instance. Therefore, the instance will process all new intent instances. "Singleinstance alimama_pid =" Courier "; alimama_titlecolor =" 3366cc "; alimama_descolor =" 000000 "; alimama_bgcolor =" ffffff "; alimama_bordercolor =" cccccc ";
Alimama_linkcolor = "008000"; alimama_bottomcolor = "ffffff"; alimama_anglesize = "0"; bandwidth = "0"; alimama_icon = "0"; bandwidth = "9999"; alimama_width = 468; alimama_height = 90; alimama_type = 2;

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.