如果假设A是Standard,B是SingleTop,C是SingleTask,D是SingleInstance的启动模式,那么以A->B->C->D->A->B->C->D这种情况开启Activity,分析一下最后的工作栈是怎样的情况,第二次的A是和ABC在同一个工作栈中还是和第一次的D在同一个工作栈中.
这是在刚哥的小密圈中,一个球友提问的。仔细看了,球员的解答,和自己的想法不同;于是在闲暇的时间又阅读了一遍Activity的四大启动模式,这里不在详细解读,请看彻底弄懂Activity四大启动模式 ,这个介绍比较清楚。
下面把结果公布,经测试有两种情况:一种SingleTask未设置taskAffinity,A、B、C在同一个工作栈中:
Task id #1844
* TaskRecord{7f90b08 #1844 A=com.example.demo U=0 sz=1}
* Hist #0: ActivityRecord{1ac3bbd5 u0 com.example.demo/.SingleInstanceActivity t1844}
Task id #1843
* TaskRecord{34b46cc6 #1843 A=com.example.demo U=0 sz=3}
* Hist #2: ActivityRecord{28c92660 u0 com.example.demo/.SingleTaskActivity t1843}
* Hist #1: ActivityRecord{70f2f65 u0 com.example.demo/.SingleTopActivity t1843}
* Hist #0: ActivityRecord{221e98a u0 com.example.demo/.MainActivity t1843}
下面是日志打印情况:
02-15 16:36:55.369 25135-25135/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 16:36:55.370 25135-25135/com.example.demo I/BaseActivity: onCreate: MainActivity, taskId: 1843, hashCode: 664113674
02-15 16:36:55.371 25135-25135/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 16:38:44.558 25135-25135/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 16:38:44.558 25135-25135/com.example.demo I/BaseActivity: onCreate: SingleTopActivity, taskId: 1843, hashCode: 359362156
02-15 16:38:44.559 25135-25135/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 16:38:47.315 25135-25135/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 16:38:47.315 25135-25135/com.example.demo I/BaseActivity: onCreate: SingleTaskActivity, taskId: 1843, hashCode: 13302767
02-15 16:38:47.316 25135-25135/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 16:38:48.693 25135-25135/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 16:38:48.695 25135-25135/com.example.demo I/BaseActivity: onCreate: SingleInstanceActivity, taskId: 1844, hashCode: 643766463
02-15 16:38:48.696 25135-25135/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 16:38:50.879 25135-25135/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 16:38:50.880 25135-25135/com.example.demo I/BaseActivity: onCreate: MainActivity, taskId: 1843, hashCode: 237435118
02-15 16:38:50.881 25135-25135/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 16:38:52.497 25135-25135/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 16:38:52.498 25135-25135/com.example.demo I/BaseActivity: onCreate: SingleTopActivity, taskId: 1843, hashCode: 249760441
02-15 16:38:52.498 25135-25135/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 16:38:53.315 25135-25135/com.example.demo I/BaseActivity: *****onNewIntent()*****
02-15 16:38:53.316 25135-25135/com.example.demo I/BaseActivity: onNewIntent: SingleTaskActivity, taskId: 1843, hashCode: 13302767
02-15 16:38:53.317 25135-25135/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 16:38:56.931 25135-25135/com.example.demo I/BaseActivity: *****onNewIntent()*****
02-15 16:38:56.932 25135-25135/com.example.demo I/BaseActivity: onNewIntent: SingleInstanceActivity, taskId: 1844, hashCode: 643766463
02-15 16:38:56.933 25135-25135/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
一种SingleTask设置taskAffinity,A、B和C不在同一个工作栈中,下面是工作栈情况:
Task id #1841
* TaskRecord{2460d850 #1841 A=com.example.demo U=0 sz=1}
* Hist #0: ActivityRecord{20da0b54 u0 com.example.demo/.SingleInstanceActivity t1841}
Task id #1840
* TaskRecord{454ef4e #1840 A=com.example.demo.singleTask U=0 sz=1}
* Hist #0: ActivityRecord{1a0ba3ea u0 com.example.demo/.SingleTaskActivity t1840}
Task id #1839
* TaskRecord{1d67d86f #1839 A=com.example.demo U=0 sz=4}
* Hist #3: ActivityRecord{2005c3b4 u0 com.example.demo/.SingleTopActivity t1839}
* Hist #2: ActivityRecord{25154369 u0 com.example.demo/.MainActivity t1839}
* Hist #1: ActivityRecord{1a361492 u0 com.example.demo/.SingleTopActivity t1839}
* Hist #0: ActivityRecord{20a0bb43 u0 com.example.demo/.MainActivity t1839}
下面是日志打印情况:
02-15 15:54:56.949 22944-22944/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 15:54:56.965 22944-22944/com.example.demo I/BaseActivity: onCreate: MainActivity, taskId: 1839, hashCode: 664113674
02-15 15:54:56.966 22944-22944/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 15:55:24.121 22944-22944/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 15:55:24.122 22944-22944/com.example.demo I/BaseActivity: onCreate: SingleTopActivity, taskId: 1839, hashCode: 6742393
02-15 15:55:24.122 22944-22944/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 15:55:27.991 22944-22944/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 15:55:27.991 22944-22944/com.example.demo I/BaseActivity: onCreate: SingleTaskActivity, taskId: 1840, hashCode: 163004873
02-15 15:55:27.992 22944-22944/com.example.demo I/BaseActivity: taskAffinity: com.example.demo.singleTask
02-15 15:55:31.177 22944-22944/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 15:55:31.178 22944-22944/com.example.demo I/BaseActivity: onCreate: SingleInstanceActivity, taskId: 1841, hashCode: 13155865
02-15 15:55:31.179 22944-22944/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 15:55:35.587 22944-22944/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 15:55:35.587 22944-22944/com.example.demo I/BaseActivity: onCreate: MainActivity, taskId: 1839, hashCode: 109388016
02-15 15:55:35.589 22944-22944/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 15:55:38.907 22944-22944/com.example.demo I/BaseActivity: *****onCreate()*****
02-15 15:55:38.908 22944-22944/com.example.demo I/BaseActivity: onCreate: SingleTopActivity, taskId: 1839, hashCode: 300899587
02-15 15:55:38.908 22944-22944/com.example.demo I/BaseActivity: taskAffinity: com.example.demo
02-15 15:55:43.743 22944-22944/com.example.demo I/BaseActivity: *****onNewIntent()*****
02-15 15:55:43.744 22944-22944/com.example.demo I/BaseActivity: onNewIntent: SingleTaskActivity, taskId: 1840, hashCode: 163004873
02-15 15:55:43.745 22944-22944/com.example.demo I/BaseActivity: taskAffinity: com.example.demo.singleTask
02-15 15:55:47.884 22944-22944/com.example.demo I/BaseActivity: *****onNewIntent()*****
02-15 15:55:47.885 22944-22944/com.example.demo I/BaseActivity: onNewIntent: SingleInstanceActivity, taskId: 1841, hashCode: 13155865
02-15 15:55:47.885 22944-22944/com.example.demo I/BaseActivity: taskAffinity: com.example.demo