Android task and back stack details 5: Task affinity and others

Source: Internet
Author: User
Processing Task affinity

Affinity indicates the task "desired" by an activity. by default, all activities of the same application have the same task affinity. therefore, by default, all activitie of an application belongs to the same task first. however, you can modify the default task affinity of an activity. activitie defined in different applications can have the same task affinity, or activitie in the same application can be assigned different task affinity.

You can use the taskaffinity attribute of the <activity> element to modify the task affinity of an activity. the taskaffinity attribute uses a string as the value. This string must be different from the default package name declared in <manifest> because the system uses the package name to identify the default task affinity.

Affinity works in the following two situations:

  • When the intent that starts an activity contains the flag_activity_new_task flag.

    By default, a new activity is placed in the task where the activity that calls startactivity () is located. however, if the intent passed to startactivity () contains the flag_activity_new_task flag, the system will find another task that can hold the new activity. generally, it is a new task. however, this is not necessary. if an existing task has the same affinity as the new activity, the activity is started and placed in the existing task. if there is no such task, a new task is started.

    If this flag causes an activity to start in a new task and the user presses the Home Key to leave the new task, there must be some ways for the user to return to the task. some entities (such as the notification manager) always start the activity in another task and never as part of their own task, so it always sets flag_activity_new_task to intents passed to startactivity. if you have an activity that can be called by external entities using this flag, be careful that the user may return to the started task in an independent way, for example, use the start icon (the root activity of the task has a category_launcherintent
    Filter).-The translation is quite uncomfortable. That is to say, as long as the same affinity is used, the user can return to the started task.

  • When the allowtaskreparenting attribute of an activity is "true.

    In this case, the activity can be moved from the task that starts it to an affinity task when the next task comes to the foreground.

    For example, assume that an activity that reports the weather conditions of the selected city is part of a tourism application. it has the same affinity with other activities in the same application (Default Application affinity) and is allowed to recognize its parent. when one of your activities starts this weather forecast activity, it initially belongs to the same task as your actvity. however, when the travel application task enters the foreground, the weather forecast activity is reassigned to this task and only displayed on it.

Tips:: If. APKThe file contains multiple"Application", You may wantActivityAttributeTaskaffinityTo connect them to different"Application".

Clear the back Stack

If the user leaves a task for a long time, the system will clear all other activities except the root activity in the task. when the user returns this task again, only the root activity is restored. the reason for the system to do this is that after a period of time, users may have abandoned what they have done and returned to the task to start doing new things.

There are some activity attributes that you can use to change such behavior:

  • Alwaysretaintaskstate

    If this attribute of the Root Activity of the task is set to "true", the default behavior described above will not happen. The task will keep all the activities in the back stack, even after a long period of time.

  • Cleartaskonlaunch

    If the attribute of the Root Activity of the task is set to "true", when the user leaves the task and returns, the stack is cleared to only the root activity. in other words, it is opposite to alwaysretaintaskstate. when you return to the task, you will always see the initial state, even if you only leave for a short time.

  • Finishontasklaunch

    This attribute is similar to cleartaskonlaunch, But it acts on a separate activity rather than the entire task. it can also cause the death of any activity, including the root activity. when it is set to "true", the activity only exists in the current session. if the user leaves and returns, it will be no longer there.

Start a task

You can set an activity as the entry of a task and set it to "android. intent. action. main "intent filter" and a value of "android. intent. category. launcher "filter. for example:

<Activity...>

<Intent-filter...>

<Actionandroid: Name = "android. Intent. Action. Main"/>

<Categoryandroid: Name = "android. Intent. Category. launcher"/>

</Intent-filter>

...

</Activity>

An intent filter causes an icon and tag of the activity to be displayed on the application startup interface, allowing the user to start the activity and return to the task again.

This second capability is very important: the user must be able to leave a task and then return through the starter. for this reason, the two startup modes enable the activity to always start in the new task: "singletask" and "singleinstance", which should be used only when the activity has the action_main and category_launcher filters. imagine, for example, what will happen without these filters: an intent starts a "singletask" activity and initializes it in a new task, in addition, the user has been busy for some time in this task. then the user presses the Home button. the task is now moved to the background and invisible. because this activity is not the app's startup activity, the user can no longer return to this task.

But what if you don't want the user to return to an activity? You can set the finishontasklaunch attribute of the <activity> element to "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.