Some of Android's practices on task Singletask, SingleInstance and Taskaffinity

Source: Internet
Author: User

The previous article gave a rough introduction to the basics of task in Android. Just practice is the only standard to test truth, so. Try today to test whether the Launchmode in the task really implements what the document says.

First of all. Define three activity. Mainactivity Open secondactivity,secondactivity to open the thirdactivity, as seen in the following:


Following. We define launch Mode for second activity. There are several situations:

1) standard (i.e. not defined) is first by default. Nothing is defined. For example, the following:

    <application        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@ String/app_name "        android:theme=" @style/apptheme ">        <activity            android:name=" Com.lms.taskdemo.MainActivity "            android:label=" @string/app_name ">            <intent-filter>                < Action android:name= "Android.intent.action.MAIN"/>                <category android:name= " Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>        <activity            android:name= "com.lms.taskdemo.SecondActivity" >        </activity>        <activity android:name= "Com.lms.taskdemo.ThirdActivity" >        </activity>    </application>
Open three activity in sequence to see the results:

04-21 15:23:28.373:v/com.lms.taskdemo (20473): task:7004-21 15:23:28.373:v/com.lms.taskdemo (20473): Base Activity:co Mponentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-21 15:23:28.373:v/com.lms.taskdemo (20473): current Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-21 15:23:28.373:v/com.lms.taskdemo ( 20473): Number of activities:104-21 15:23:28.373:v/com.lms.taskdemo (20473): Number of running activities:104-21 15:23: 31.336:v/com.lms.taskdemo (20473): task:7004-21 15:23:31.336:v/com.lms.taskdemo (20473): Base activity:componentinfo {com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-21 15:23:31.336:v/com.lms.taskdemo (20473): Current Activity: Componentinfo{com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-21 15:23:31.336:v/com.lms.taskdemo (20473): Number of activities:204-21 15:23:31.336:v/com.lms.taskdemo (20473): Number of running activities:204-21 15:23:33.999: V/com.lms.taskdemo (20473): Task:7004-21 15:23:33.999:v/com.lms.Taskdemo (20473): Base activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-21 15:23:33.999:v/ Com.lms.taskdemo (20473): Current activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.thirdactivity}04-21 15:23:33.999:v/com.lms.taskdemo (20473): Number of activities:304-21 15:23:33.999:v/com.lms.taskdemo (20473): number of running Activities:3

Can see three activity, all in the same task.

2) Singletask

For second Activity, define its launch mode as "Singletask". For example, the following:

    <application        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@ String/app_name "        android:theme=" @style/apptheme ">        <activity            android:name=" Com.lms.taskdemo.MainActivity "            android:label=" @string/app_name ">            <intent-filter>                < Action android:name= "Android.intent.action.MAIN"/>                <category android:name= " Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>        <activity            android:name= "com.lms.taskdemo.SecondActivity"            android:launchmode= "Singletask" >        </ activity>        <activity android:name= "com.lms.taskdemo.ThirdActivity" >        </activity>    </application>

Then look at the results, such as the following:

04-22 10:31:26.197:v/com.lms.taskdemo (924): Task:16204-22 10:31:26.217:v/com.lms.taskdemo (924): Base Activity:compo Nentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:31:26.217:v/com.lms.taskdemo (924): current Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:31:26.217:v/com.lms.taskdemo (924  ): Number of activities:104-22 10:31:26.217:v/com.lms.taskdemo (924): Number of running activities:104-22 10:31:31.112: V/com.lms.taskdemo (924): Task:16204-22 10:31:31.112:v/com.lms.taskdemo (924): Base activity:componentinfo{ Com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:31:31.112:v/com.lms.taskdemo (924): Current Activity: Componentinfo{com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:31:31.112:v/com.lms.taskdemo (924): Number of activities:204-22 10:31:31.112:v/com.lms.taskdemo (924): Number of running activities:204-22 10:31:32.884:v/ Com.lms.taskdemo (924): Task:16204-22 10:31:32.884:v/com.lms.taskdemo (924): Base Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:31:32.884:v/com.lms.taskdemo (924 ): Current Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.thirdactivity}04-22 10:31:32.884:v/  Com.lms.taskdemo (924): Number of activities:304-22 10:31:32.884:v/com.lms.taskdemo (924): Number of running activities: 2

Discover that although Singletask is defined, second activity and main activity are in the same task. It doesn't match what we expected. The reason in fact is that Launchmode = Singletask initiated activity, at boot time, will find the same task as its taskaffinity, assuming there is such a task, start in this task, Assume that a task does not exist to create a new task and start it in a new task.

In this case, because the taskaffinity is not defined for each activity, the default affinity value is the package name, and a few activity is the same, so it is started in the same task.

3) Single Task Different task Affinity

Following. We set different taskaffinity values for main activity and Second activity, respectively, such as the following:

 <application android:allowbackup= "true" android:icon= "@drawable/ic_laun Cher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity A Ndroid:name= "com.lms.taskdemo.MainActivity" android:label= "@string/app_name" android:taskaffinity= "C Om.lms.taskdemo.MainActivity "> <intent-filter> <action android:name=" android.intent . Action. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte r> </activity> <activity android:name= "Com.lms.taskdemo.SecondActivity" an Droid:launchmode= "Singletask" android:taskaffinity= "com.lms.taskdemo.SecondActivity" > </activity&        Gt <activity android:name= "com.lms.taskdemo.ThirdActivity" > </activity> </application> 

Here is the result, the order of initiation is Main activity->second activity->main activity, Second activity, third activity

04-22 10:26:27.818:v/com.lms.taskdemo (32677): Task:15104-22 10:26:27.818:v/com.lms.taskdemo (32677): Base activity:c Omponentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:26:27.818:v/com.lms.taskdemo (32677): Current Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:26:27.818:v/ Com.lms.taskdemo (32677): Number of activities:104-22 10:26:27.818:v/com.lms.taskdemo (32677): Number of running Activit Ies:104-22 10:26:31.362:v/com.lms.taskdemo (32677): Task:15204-22 10:26:31.362:v/com.lms.taskdemo (32677): Base Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:26:31.362:v/com.lms.taskdemo ( 32677): Current activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:26:31.372:v/ Com.lms.taskdemo (32677): Number of activities:104-22 10:26:31.372:v/com.lms.taskdemo (32677): Number of running Activit Ies:104-22 10:26:36.558:v/com.lms.taskdemo (32677): Task:15104-22 10:26:36.558:v/com. Lms.taskdemo (32677): Base activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:26:36.558:v/com.lms.taskdemo (32677): Current activity:componentinfo{com.lms.taskdemo/ Com.lms.taskdemo.mainactivity}04-22 10:26:36.558:v/com.lms.taskdemo (32677): Number of Activities:104-22 10:26:36.558:v/com.lms.taskdemo (32677): Number of running activities:104-22 10:26:39.401:v/com.lms.taskdemo (32677): Task:15304-22 10:26:39.401:v/com.lms.taskdemo (32677): Base activity:componentinfo{com.lms.taskdemo/ Com.lms.taskdemo.secondactivity}04-22 10:26:39.401:v/com.lms.taskdemo (32677): Current activity:componentinfo{ Com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:26:39.401:v/com.lms.taskdemo (32677): Number of Activities:104-22 10:26:39.401:v/com.lms.taskdemo (32677): Number of running activities:104-22 10:26:41.123:v/com.lms . Taskdemo (32677): Task:15304-22 10:26:41.123:v/com.lms.taskdemo (32677): Base activity:componentinfo{ Com.lms.taskdemo/com.lms.taskdEmo. Secondactivity}04-22 10:26:41.123:v/com.lms.taskdemo (32677): Current activity:componentinfo{com.lms.taskdemo/ Com.lms.taskdemo.thirdactivity}04-22 10:26:41.123:v/com.lms.taskdemo (32677): Number of Activities:204-22 10:26:41.123:v/com.lms.taskdemo (32677): Number of running Activities:2

In the above results, you can see that when you start second activity, it is actually started in a new task. Second activity is the root activity in this task. The third activity initiated by the second activity is also initiated in this task.

4) Single Instance below, let's take a look at single Instance, the same, don't define task Affinity first. The following are defined in the Androidmanifest file as:
    <application        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@ String/app_name "        android:theme=" @style/apptheme ">        <activity            android:name=" Com.lms.taskdemo.MainActivity "            android:label=" @string/app_name ">            <intent-filter>                < Action android:name= "Android.intent.action.MAIN"/>                <category android:name= " Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>        <activity            android:name= "com.lms.taskdemo.SecondActivity"            android:launchmode= "singleinstance" >        </ activity>        <activity android:name= "com.lms.taskdemo.ThirdActivity" >        </activity>    </application>

Here are the results:
04-22 10:29:43.207:v/com.lms.taskdemo (682): Task:15904-22 10:29:43.207:v/com.lms.taskdemo (682): Base Activity:compo Nentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:29:43.207:v/com.lms.taskdemo (682): Current Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:29:43.207:v/com.lms.taskdemo (682  ): Number of activities:104-22 10:29:43.207:v/com.lms.taskdemo (682): Number of running activities:104-22 10:29:46.580: V/com.lms.taskdemo (682): Task:16004-22 10:29:46.580:v/com.lms.taskdemo (682): Base activity:componentinfo{ Com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:29:46.580:v/com.lms.taskdemo (682): Current Activity: Componentinfo{com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:29:46.580:v/com.lms.taskdemo (682): Number of activities:104-22 10:29:46.580:v/com.lms.taskdemo (682): Number of running activities:104-22 10:29:48.202:v/ Com.lms.taskdemo (682): Task:15904-22 10:29:48.202:v/com.lms.taskdemo (682): BaSe activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:29:48.202:v/com.lms.taskdemo ( 682): Current activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.thirdactivity}04-22 10:29:48.202:v/  Com.lms.taskdemo (682): Number of activities:204-22 10:29:48.202:v/com.lms.taskdemo (682): Number of running activities: 104-22 10:29:57.112:v/com.lms.taskdemo (682): Task:16004-22 10:29:57.112:v/com.lms.taskdemo (682): Base Activity:comp Onentinfo{com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:29:57.112:v/com.lms.taskdemo (682): Current Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:29:57.112:v/com.lms.taskdemo ( 682): Number of activities:104-22 10:29:57.112:v/com.lms.taskdemo (682): Number of running activities:1
The ability to see the startup second activity is in a new task, and when the third activity is started, it runs back to the original task. This is in line with the description of the document. It is true that there is only one activity in the newly created task.

And because we don't define task Affinity, when we start third activity. It will look for the same affinity task, so it will find the original task. In other words, assuming that the taskaffinity is defined, the third activity should be created in the new task, and the following will be verified.


5) Single Instance Different affinity defined in the configuration file such as the following:
 <application android:allowbackup= "true" android:icon= "@drawable/ic_laun Cher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity A Ndroid:name= "com.lms.taskdemo.MainActivity" android:label= "@string/app_name" android:taskaffinity= "C Om.lms.taskdemo.MainActivity "> <intent-filter> <action android:name=" android.intent . Action. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte r> </activity> <activity android:name= "Com.lms.taskdemo.SecondActivity" an Droid:launchmode= "SingleInstance" android:taskaffinity= "com.lms.taskdemo.SecondActivity" > </activ ity> <activity android:name= "com.lms.taskdemo.ThirdActivity" > </activity> </applicatio N> 

Take a look at the results, such as the following:
04-22 10:27:58.866:v/com.lms.taskdemo (391): Task:15504-22 10:27:58.866:v/com.lms.taskdemo (391): Base activity:compo Nentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:27:58.866:v/com.lms.taskdemo (391): Current Activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.mainactivity}04-22 10:27:58.866:v/com.lms.taskdemo (391  ): Number of activities:104-22 10:27:58.866:v/com.lms.taskdemo (391): Number of running activities:104-22 10:28:00.677: V/com.lms.taskdemo (391): Task:15604-22 10:28:00.677:v/com.lms.taskdemo (391): Base activity:componentinfo{ Com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:28:00.677:v/com.lms.taskdemo (391): Current Activity: Componentinfo{com.lms.taskdemo/com.lms.taskdemo.secondactivity}04-22 10:28:00.677:v/com.lms.taskdemo (391): Number of activities:104-22 10:28:00.677:v/com.lms.taskdemo (391): Number of running activities:104-22 10:28:02.309:v/ Com.lms.taskdemo (391): Task:15704-22 10:28:02.309:v/com.lms.taskdemo (391): BaSe activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.thirdactivity}04-22 10:28:02.309:v/com.lms.taskdemo ( 391): Current activity:componentinfo{com.lms.taskdemo/com.lms.taskdemo.thirdactivity}04-22 10:28:02.309:v/  Com.lms.taskdemo (391): Number of activities:104-22 10:28:02.309:v/com.lms.taskdemo (391): Number of running activities: 1

Sure enough As we would expect, the current activity is started in a new task.


Summary: When using launch Mode to change the system default task scheduling. If you use a single task or single instance, you should also note the use of affinity, with affinity. It is possible to achieve the desired effect. Affinity, in fact, is a table-dependent reference for Android, similar to a tag value, which indicates which tag the current activity belongs to, and the activity of the equivalent affinity value. Assuming that no other flags are used, such as single instance, it will be present in the same task. Under normal circumstances, we do not define a task affinity value. The default value is the package name of the current app.
End.

Some of Android's practices on task Singletask, SingleInstance and Taskaffinity

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.