上一篇文章粗略地介绍了一下关于Android中Task的基本知识,不过实践才是检验真理的唯一标准,所以,今天就来试验一下Task中的launchMode是否真的实现了文档所说的那样。
首先,定义三个Activity,MainActivity打开SecondActivity,SecondActivity打开ThirdActivity,如下所示:
下面,我们定义Second Activity的Launch Mode,分别有下面几种情况:
顺序打开三个Activity,看看结果:
04-21 15:23:28.373: V/com.lms.taskdemo(20473): Task : 70
04-21 15:23:28.373: V/com.lms.taskdemo(20473): Base activity : ComponentInfo{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 : 1
04-21 15:23:28.373: V/com.lms.taskdemo(20473): Number of running activities: 1
04-21 15:23:31.336: V/com.lms.taskdemo(20473): Task : 70
04-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 : 2
04-21 15:23:31.336: V/com.lms.taskdemo(20473): Number of running activities: 2
04-21 15:23:33.999: V/com.lms.taskdemo(20473): Task : 70
04-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 : 3
04-21 15:23:33.999: V/com.lms.taskdemo(20473): Number of running activities: 3
为Second Activity,定义其launch mode 为 “singleTask”,如下:
04-22 10:31:26.197: V/com.lms.taskdemo(924): Task : 162
04-22 10:31:26.217: V/com.lms.taskdemo(924): Base activity : ComponentInfo{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 : 1
04-22 10:31:26.217: V/com.lms.taskdemo(924): Number of running activities: 1
04-22 10:31:31.112: V/com.lms.taskdemo(924): Task : 162
04-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 : 2
04-22 10:31:31.112: V/com.lms.taskdemo(924): Number of running activities: 2
04-22 10:31:32.884: V/com.lms.taskdemo(924): Task : 162
04-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 : 3
04-22 10:31:32.884: V/com.lms.taskdemo(924): Number of running activities: 2
而在这里,因为没有为各个activity定义taskAffinity,那么默认的affinity值就是包名,那么几个Activity都是一样的,所以就在同样的Task中启动了。
下面,我们分别为Main Activity和 Second Activity设置不同的TaskAffinity值,如下:
04-22 10:26:27.818: V/com.lms.taskdemo(32677): Task : 151
04-22 10:26:27.818: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{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 : 1
04-22 10:26:27.818: V/com.lms.taskdemo(32677): Number of running activities: 1
04-22 10:26:31.362: V/com.lms.taskdemo(32677): Task : 152
04-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 : 1
04-22 10:26:31.372: V/com.lms.taskdemo(32677): Number of running activities: 1
04-22 10:26:36.558: V/com.lms.taskdemo(32677): Task : 151
04-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 : 1
04-22 10:26:36.558: V/com.lms.taskdemo(32677): Number of running activities: 1
04-22 10:26:39.401: V/com.lms.taskdemo(32677): Task : 153
04-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 : 1
04-22 10:26:39.401: V/com.lms.taskdemo(32677): Number of running activities: 1
04-22 10:26:41.123: V/com.lms.taskdemo(32677): Task : 153
04-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 : 2
04-22 10:26:41.123: V/com.lms.taskdemo(32677): Number of running activities: 2
04-22 10:29:43.207: V/com.lms.taskdemo(682): Task : 159
04-22 10:29:43.207: V/com.lms.taskdemo(682): Base activity : ComponentInfo{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 : 1
04-22 10:29:43.207: V/com.lms.taskdemo(682): Number of running activities: 1
04-22 10:29:46.580: V/com.lms.taskdemo(682): Task : 160
04-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 : 1
04-22 10:29:46.580: V/com.lms.taskdemo(682): Number of running activities: 1
04-22 10:29:48.202: V/com.lms.taskdemo(682): Task : 159
04-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 : 2
04-22 10:29:48.202: V/com.lms.taskdemo(682): Number of running activities: 1
04-22 10:29:57.112: V/com.lms.taskdemo(682): Task : 160
04-22 10:29:57.112: V/com.lms.taskdemo(682): Base activity : ComponentInfo{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 : 1
04-22 10:29:57.112: V/com.lms.taskdemo(682): Number of running activities: 1
可以看到启动Second Activity的时候,是在新的Task中,而当启动Third Activity的时候,它又跑回原来的Task中去了,这是跟文档描述中符合的,的确是新建的Task中只能有且仅有一个Activity。而由于我们没有定义Task Affinity,所以当启动Third Activity的时候,它就会去找相同Affinity的task,所以就会找到原来的Task,也即是说,如果定义了TaskAffinity的话,那以Third Activity就应该在新的Task中创建了,下面就来验证一下。
04-22 10:27:58.866: V/com.lms.taskdemo(391): Task : 155
04-22 10:27:58.866: V/com.lms.taskdemo(391): Base activity : ComponentInfo{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 : 1
04-22 10:27:58.866: V/com.lms.taskdemo(391): Number of running activities: 1
04-22 10:28:00.677: V/com.lms.taskdemo(391): Task : 156
04-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 : 1
04-22 10:28:00.677: V/com.lms.taskdemo(391): Number of running activities: 1
04-22 10:28:02.309: V/com.lms.taskdemo(391): Task : 157
04-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 : 1
04-22 10:28:02.309: V/com.lms.taskdemo(391): Number of running activities: 1