Activity启动模式之SingleTask

启动模式系列之:Activity启动模式(Standrd和SingleTop)
启动模式系列之:Activity启动模式(SingleTask)
启动模式系列之:Activity启动模式(SingleInstance)

一,SingleTask模式(栈内复用模式)

解释:只要在栈(TaskRecord)内,存在栈内复用模式的Activity。无论是在栈顶还是在栈内,多次调用都会复用实例。不会重复创建。除此之外栈内复用模式还可以设置不同的任务栈,即taskAffinity属性。

二,未设置taskAffinity属性

解释:没有设置taskAffinity属性。启动该Activity会将该Activity压入上个Activity所在的TaskRecord任务栈中。还有另外一个特点,如果当前Activity已经在栈内,再次调起该Activity到栈顶。他会清除其上面的其他实例。具有清除顶部(clearTop)的效果。

操作示例:

MainActivity -> TestAActivity -> TestBActivity -> TestCActivity 此时TestBActivity位于栈中

adb记录如下:

  Stack #1:
    Task id #616
      TaskRecord{3179bb90 #616 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=4}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
        Hist #3: ActivityRecord{2b528c2 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t616}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity }
          ProcessRecord{12ddadbf 14756:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #2: ActivityRecord{1c88e63f u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t616}
          Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
          ProcessRecord{12ddadbf 14756:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #1: ActivityRecord{166a7561 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t616}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
          ProcessRecord{12ddadbf 14756:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{34e873fa u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t616}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
          ProcessRecord{12ddadbf 14756:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}

    Running activities (most recent first):
      TaskRecord{3179bb90 #616 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=4}
        Run #3: ActivityRecord{2b528c2 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t616}
        Run #2: ActivityRecord{1c88e63f u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t616}
        Run #1: ActivityRecord{166a7561 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t616}
        Run #0: ActivityRecord{34e873fa u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t616}

    mResumedActivity: ActivityRecord{2b528c2 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t616}

此时操作:

TestCActivity -> TestBActivity

因为TestBActivity是SingleTask模式栈内复用,并且具有清除顶部的效果,所以TestCActivity出栈。

adb记录如下:

 Stack #1:
    Task id #616
      TaskRecord{3179bb90 #616 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=3}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
        Hist #2: ActivityRecord{1c88e63f u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t616}
          Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
          ProcessRecord{12ddadbf 14756:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #1: ActivityRecord{166a7561 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t616}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
          ProcessRecord{12ddadbf 14756:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{34e873fa u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t616}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
          ProcessRecord{12ddadbf 14756:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}

    Running activities (most recent first):
      TaskRecord{3179bb90 #616 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=3}
        Run #2: ActivityRecord{1c88e63f u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t616}
        Run #1: ActivityRecord{166a7561 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t616}
        Run #0: ActivityRecord{34e873fa u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t616}

    mResumedActivity: ActivityRecord{1c88e63f u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t616}

从上面两个adb记录。SingleTask模式在没有设置taskAffinity属性下的使用。了然于心。

三,设置taskAffinity属性,任务栈不同。

解释:

修改AndroidManifest下TestBActivity的taskAffinity=“org.wangchenlong.stack”。taskAffinity属性适用于栈内复用模式,配合使用,在栈顶复用和标准模式中无效。

当taskAffinity所指定的任务栈不存在的时候,初次启动栈内复用模式的实例。会创建新的任务栈,并将实例放置于其中。

操作实例:

MainActivity -> TestAActivity -> TestBActivity。

此时TestB是栈内复用模式且任务栈不同。此时包含两个任务栈。

adb记录如下:

  Stack #1:
    Task id #618
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=1}
      Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
        Hist #0: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
          Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
    Task id #617
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
        Hist #1: ActivityRecord{237eb6b8 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t617}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}

    Running activities (most recent first):
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=1}
        Run #2: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
        Run #1: ActivityRecord{237eb6b8 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t617}
        Run #0: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}

    mResumedActivity: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}

从上面日志我们可以看到。有两个TaskRecord。两个TaskRecord的A值是不一样的。一个是taskAffinity的属性值,一个是应用的包名。

而此时的TestBActivity单独位于taskAffinity所设置的TaskRecorkd A=org.wangchenlong.stack中。

操作实例:

TestBActivity -> TestCActivity。

此时继续上个操作继续操作

adb记录如下:

  Stack #1:
    Task id #618
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=2}
      Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
        Hist #1: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
          Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
    Task id #617
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
        Hist #1: ActivityRecord{237eb6b8 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t617}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}

    Running activities (most recent first):
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=2}
        Run #3: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}
        Run #2: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
        Run #1: ActivityRecord{237eb6b8 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t617}
        Run #0: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}

    mResumedActivity: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}

此时我们发现,后调起的TestCActivity和调起他的TestBActivity位于同一个TaskRecord中。此时TestCActivity位于栈顶。

当任务栈不同的时候,启动不同栈内的实例,导致任务栈之间的切换,后台任务栈会位于前台。

如按手机Home键,然后再次启动应用(点应用图标而不是在后台应用列表中点),则默认任务栈会启动,并不是原有(taskAffinity设置)的任务栈。则栈顶元素由TestCActivity变为TestAActivity

adb记录如下:

  Stack #1:
    Task id #617
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
        Hist #1: ActivityRecord{237eb6b8 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t617}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
    Task id #618
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=2}
      Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
        Hist #1: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity bnds=[87,114][492,803] }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
          Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}

    Running activities (most recent first):
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
        Run #3: ActivityRecord{237eb6b8 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t617}
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=2}
        Run #2: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}
        Run #1: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
        Run #0: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}

    mResumedActivity: ActivityRecord{237eb6b8 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t617}

正如上面说的,默认任务栈处于前台所以TestAActivity,而taskAffinity所设置的任务栈处于后台。

继续操作:

按返回键,关闭TestAActivity。则下一个页面是位于同一个任务栈TaskRecord中的MainActivity而不是其他任务栈中的TestCActivity.

adb记录如下:

  Stack #1:
    Task id #617
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=1}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
        Hist #0: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
    Task id #618
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=2}
      Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
        Hist #1: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity bnds=[87,114][492,803] }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
          Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}

    Running activities (most recent first):
      TaskRecord{6aab849 #617 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=1}
        Run #2: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=2}
        Run #1: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}
        Run #0: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}

    mResumedActivity: ActivityRecord{3ca4e2c5 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t617}

正如上面所说的,位于栈顶的是MainActivity。

继续操作:

按返回键退出MainActivity.将会退至桌面,而不是切换到当前应用的其他任务栈。

adb记录如下:

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
  Stack #0:
    Task id #551
      TaskRecord{39fa9a9c #551 A=smartisanos.task.launcher U=0 sz=1}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10800000 cmp=com.smartisanos.launcher/.Launcher }
        Hist #0: ActivityRecord{2e9a3166 u0 com.smartisanos.launcher/.Launcher t551}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10800000 cmp=com.smartisanos.launcher/.Launcher }
          ProcessRecord{1db6d97c 5026:com.smartisanos.launcher/u0a12}

    Running activities (most recent first):
      TaskRecord{39fa9a9c #551 A=smartisanos.task.launcher U=0 sz=1}
        Run #0: ActivityRecord{2e9a3166 u0 com.smartisanos.launcher/.Launcher t551}

    mResumedActivity: ActivityRecord{2e9a3166 u0 com.smartisanos.launcher/.Launcher t551}

  Stack #1:
    Task id #618
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=2}
      Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
        Hist #1: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity bnds=[87,114][492,803] }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
          Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}

    Running activities (most recent first):
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=2}
        Run #1: ActivityRecord{6d922ae u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t618}
        Run #0: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}

从上面日志可以看出来,当退出MainActivity,等于把桌面栈添加到序列中。所以当前系统的Activity栈是有两个,一个是桌面应用栈,一个是我们的测试应用栈,但是桌面栈位于栈顶。所以我们当前位于桌面。

但是我们同时看到。测试应用虽然已经退出了。但是我们创建的taskAffinity属性的任务栈仍然存在。
是为了避免重复创建,复合栈内复用模式。

继续操作:

MainActivity -> TestAActivity -> TestBActivity

因为TestBActivity所在的栈仍然在内存中。所以直接切换到TestBActivity所在的任务栈中。

adb日志记录:

  Stack #1:
    Task id #618
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=1}
      Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
        Hist #0: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
          Intent { flg=0x10000000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
    Task id #621
      TaskRecord{20d1d6fd #621 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
      Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
        Hist #1: ActivityRecord{1d9a8ad0 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t621}
          Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
        Hist #0: ActivityRecord{26c3345d u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t621}
          Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
          ProcessRecord{1d43244d 19007:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}

    Running activities (most recent first):
      TaskRecord{1fbe5e50 #618 A=org.wangchenlong.stack U=0 sz=1}
        Run #2: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}
      TaskRecord{20d1d6fd #621 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
        Run #1: ActivityRecord{1d9a8ad0 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t621}
        Run #0: ActivityRecord{26c3345d u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t621}

    mResumedActivity: ActivityRecord{2ae1cfce u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t618}

从日志看出来,TestBActivity所在的任务栈位于栈顶,而TestCActivity因为TestBActivity的clearTop机制,所以taskAffinity属性设置的任务栈只有TestBActivity。其实TestBActivity并没有新创建,而是复用了之前任务栈的中的TestBActivity,从TestBActivity的声明周期我们能看到TestBActivity并没有执行onCreate()方法。而是直接走了onNewIntent()。

以上,应该将SingleTask模式的栈内复用机制描述的很清楚了。

Demo下载:https://download.csdn.net/download/wangxp423/10767068

参考:《高级Android开发强化实战》

推荐:https://github.com/wangxp423 喜欢的小伙伴点一下 Star

你可能感兴趣的:(Android相关)