Android's taskaffinity+allowtaskreparenting

Source: Internet
Author: User

Taskaffinity: Specifies the host object (Task) of the activity.

You can specify an instance object of the task that the activity itself needs to enter, and if not specified, the default is to use the taskaffinity in application (the default is the package name). Note the string that sets the Taskaffinity setting requires a prefix. taskaffinity= ". Youname". Otherwise the error may be install_parse_failed_manifest_malformed.

In this document there are actually a lot of talk: http://developer.android.com/guide/topics/manifest/activity-element.html

Here are some examples to illustrate, and some questions to solve.

Experiment one: Launch the Launchmodetest1, Launchmodetest2,launchmodetest1 of two applications. Mainactivity remains in the default task (Launchmodetest1), Launchmodetest2. Mainactivity remains in the default task (LAUNCHMODETEST2), and Launchmodetest1. Otheractivity1+launchmodetest2. The OtherActivity1 remains in the task (launchmodetest).

You can specify the instance object of the task that the activity itself needs to enter, and if not specified, the default package name. Note the string that sets the Taskaffinity setting requires a prefix. taskaffinity= ". Youname". Otherwise it will error install_parse_failed_manifest_malformed.

Pre-preparation, hit log related.

1<uses-permission android:name= "Android.permission.GET_TASKS"/>2Private FinalString TAG =getclass (). GetName ();3 @Override4     protected voidOnresume () {5         NewHandler (). postdelayed (NewRunnable () {6 @Override7              Public voidrun () {8Activitymanager Manager =(Activitymanager) Getsystemservice (activity_service);9list<runningtaskinfo> Runningtaskinfos = manager.getrunningtasks (1);Ten                 if(NULL!=Runningtaskinfos) { OneRunningtaskinfo info = runningtaskinfos.get (0); ALOG.E (TAG, "task ID:" +info.id+ "Base:" +info.baseActivity.toString ()); -LOG.E (TAG, "task ID:" +info.id+ "top:" +info.topActivity.toString ()); -                 } the             } -}, 500); -         Super. Onresume (); -}

Project:com.example.launchmodetest1/com.example.launchmodetest2.

Activities:com.example.launchmodetest1.mainactivity/com.example.launchmodetest2.mainactivity

Mainactivity:taskaffinity= ". Launchmodetest".

Project runs Com.example.launchmodetest1, and then home:

01-08 22:26:43.915:e/com.example.launchmodetest1.mainactivity (27622): Task id:302 base:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.mainactivity}
01-08 22:26:43.915:e/com.example.launchmodetest1.mainactivity (27622): Task id:302 top:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.mainactivity}

Project Run Com.example.launchmodetest2:

01-08 22:27:11.695:e/com.example.launchmodetest2.mainactivity (27810): Task id:302 base:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.mainactivity}
01-08 22:27:11.695:e/com.example.launchmodetest2.mainactivity (27810): Task id:302 top:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}

Ok,launchmodetest2 enters the launchmodetest1 stack, but when you restart the app, it starts with launchmodetest1, and the result is not the same as before. The environment is not very pure, ADB activation ACTIVITY will take parameter intent.flag_activity_new_task, resulting in discrepancies.

New activities:com.example.launchmodetest1.otheractivity1/ Com.example.launchmodetest2.otheractivity1,mainactivity start otheractivity1,otheractivity1:taskaffinity= ". Launchmodetest ".

Using StartActivity (New Intent ("Com.example.launchmodetest1.OtherActivity1"), StartActivity (New Intent (" Com.example.launchmodetest1.OtherActivity1 "), the test does not pass.

Start: StartActivity (New Intent ("Com.example.launchmodetest1.OtherActivity1"). SetFlags (Intent.flag_activity_new_ TASK), StartActivity (New Intent ("Com.example.launchmodetest1.OtherActivity1"). SetFlags (intent.flag_activity_new _task)).

01-08 23:13:43.295:e/com.example.launchmodetest1.mainactivity (1495): Task id:338 base:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.mainactivity}
01-08 23:13:43.295:e/com.example.launchmodetest1.mainactivity (1495): Task id:338 top:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.mainactivity}
01-08 23:13:44.795:e/com.example.launchmodetest1.otheractivity1 (1495): Task id:341 base:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.otheractivity1}
01-08 23:13:44.795:e/com.example.launchmodetest1.otheractivity1 (1495): Task id:341 top:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.otheractivity1}
01-08 23:13:50.485:e/com.example.launchmodetest2.mainactivity (1328): Task id:342 base:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}
01-08 23:13:50.485:e/com.example.launchmodetest2.mainactivity (1328): Task id:342 top:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}
01-08 23:13:51.895:e/com.example.launchmodetest2.otheractivity1 (1328): Task id:341 base:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.otheractivity1}
01-08 23:13:51.895:e/com.example.launchmodetest2.otheractivity1 (1328): Task id:341 top:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.otheractivity1}

Test pass, Launchmodetest1. Mainactivity->launchmodetest1. Otheractivity1->home->launchmodetest2. Mainactivity->launchmodetest2. OtherActivity1

Continuous return:

01-08 23:14:42.405:e/com.example.launchmodetest1.otheractivity1 (1495): Task id:341 base:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.otheractivity1}
01-08 23:14:42.405:e/com.example.launchmodetest1.otheractivity1 (1495): Task id:341 top:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.otheractivity1}
01-08 23:14:44.015:e/com.example.launchmodetest2.mainactivity (1328): Task id:342 base:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}
01-08 23:14:44.015:e/com.example.launchmodetest2.mainactivity (1328): Task id:342 top:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}

Results: Launchmodetest2. Otheractivity1->launchmodetest1. Otheractivity1->launchmodetest2. Mainactivity, in line with expectations.

Explanation: Launchmodetest1. Mainactivity (Task (LAUNCHMODETEST1))->launchmodetest1. OtherActivity1 (Task (launchmodetest))->home (Task (launcher))->launchmodetest2. Mainactivity (Task (LAUNCHMODETEST2)))->launchmodetest2. OtherActivity1 (Task (launchmodetest)), at the end of startup, involves 4 Task,task (LAUNCHMODETEST1) under Task (launcher), Task (launcher) There are two task,task (launchmodetest2) +task (launchmodetest) above, and the task (Launchmodetest) is currently displayed. Aunchmodetest2. OtherActivity1, click Back, Return to Task (launchmodetest). Launchmodetest1. OtherActivity1, then returns, back to Task (LAUNCHMODETEST2), displays the task (LAUNCHMODETEST2). Launchmodetest2. Mainactivity, then returns, back to Task (launcher).

总结:taskAffinity+Intent.FLAG_ACTIVITY_NEW_TASK在第一次选择task时可以宿主到taskAffinity设置的task里。

Allowtaskreparenting: A re-host object (Task) that allows activity.

If an application's Activity1 launches another application's Activity2 (standard) and does not use Intent.flag_activity_new_task, then it is possible for Activity2 to complete its life cycle within that TASK, Cannot return the specified task to its original taskaffinity.

Experiment One: Activity re-hosting

Project:com.example.launchmodetest1/com.example.launchmodetest2.

Activities:com.example.launchmodetest1.MainActivity, com.example.launchmodetest1.otheractivity1/ Com.example.launchmodetest2.MainActivity

Otheractivity1:allowtaskreparenting= "true".

Start Launchmodetest2. Mainactivity->launchmodetest1. OtherActivity1

01-09 01:40:15.595:e/com.example.launchmodetest2.mainactivity (13210): Task id:429 base:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}
01-09 01:40:15.595:e/com.example.launchmodetest2.mainactivity (13210): Task id:429 top:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}
01-09 01:40:19.205:e/com.example.launchmodetest1.otheractivity1 (13080): Task id:429 base:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}
01-09 01:40:19.205:e/com.example.launchmodetest1.otheractivity1 (13080): Task id:429 top:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.otheractivity1}

Home->launchmodetest2

01-09 01:42:15.055:e/com.example.launchmodetest2.mainactivity (13210): Task id:429 base:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}
01-09 01:42:15.055:e/com.example.launchmodetest2.mainactivity (13210): Task id:429 top:componentinfo{ Com.example.launchmodetest2/com.example.launchmodetest2.mainactivity}

Home->launchmodetest1

01-09 01:42:42.515:e/com.example.launchmodetest1.otheractivity1 (13080): Task id:431 base:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.otheractivity1}
01-09 01:42:42.515:e/com.example.launchmodetest1.otheractivity1 (13080): Task id:431 top:componentinfo{ Com.example.launchmodetest1/com.example.launchmodetest1.otheractivity1}

Explanation: Launchmodetest2. Mainactivity (Task (LAUNCHMODETEST2))->launchmodetest1. OtherActivity1 (Task (LAUNCHMODETEST2))->launcher (Task (launcher))->launchmodetest2 (Task (LAUNCHMODETEST2))- >launcher->launchmodetest1 (Task (LAUNCHMODETEST1)) Launchmodetest1 at this time. OtherActivity1 Task from (Task (LAUNCHMODETEST2)) (Task (LAUNCHMODETEST1)).

Android's taskaffinity+allowtaskreparenting

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.