启动模式系列之:Activity启动模式(Standrd和SingleTop)
启动模式系列之:Activity启动模式(SingleTask)
启动模式系列之:Activity启动模式(SingleInstance)
一,启动模式分类:
- Standard(标准模式,默认)
- SingleTop(栈顶复用模式)
- SingleTask(栈内复用模式)
- SingleInstance(单实例模式)
通过AndroidManifest可配置Activity的LaunchMode.如果没有配置,默认是Standard模式。
taskAffinity属性,可设置Activity的栈。默认是当前包名。此属性仅适用于后三种启动模式。
二,ADB命令
我们通过ADB的命令,就可以直观的看到当前系统内Activity的栈结构。
具体命令如下:
adb shell dumpsys activity | sed -n -e ‘/Stack #/p’ -e ‘/Running activities/,/Run#0/p’
我们能看到如下结果:
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
Stack #1:
Task id #610
TaskRecord{36376d7d #610 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{f735322 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t610}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
ProcessRecord{2935d010 30637:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #0: ActivityRecord{2524b17c u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
ProcessRecord{2935d010 30637:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Running activities (most recent first):
TaskRecord{36376d7d #610 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=2}
Run #1: ActivityRecord{f735322 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t610}
Run #0: ActivityRecord{2524b17c u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
mResumedActivity: ActivityRecord{f735322 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t610}
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}
从Display #0 (activitys from top to bottom)下面我们能够看出有两个Stack栈。从字面意思我们也可以看出从上到下是有两个栈的,其中栈顶的Stack #1从包名就能看出里,是我测试用的栈。
而Stack #0能看出来是从桌面图标启动,所以可以认为是系统桌面Activity的栈。
从上面我们可以看出来Activity的存储结构分为三层:
- 栈(Stack),栈中可以有多个任务记录,这个栈我们可以认为是一个进程的栈,以包名来区分吧。
- 任务记录(TaskRecord),也是栈结构,后进先出。任务记录里面有多个ActivityRecord
- Activity记录(ActivityRecord),我们可以理解为就是一个Activity
三,Standrd模式(标准启动模式)
标准启动模式就是没有特殊配置的Activity。该启动模式下会一直创建Activity并将该Activity压入TaskRecord中。
示例:
MainActivity下有两个按钮,一个用来调起他自己。一个用来调起TestAActivity。都是Standrd模式。
adb记录如下:
Stack #1:
Task id #610
TaskRecord{36376d7d #610 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=5}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
Hist #4: ActivityRecord{10ff3848 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t610}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
ProcessRecord{2935d010 30637:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #3: ActivityRecord{d9bece2 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
ProcessRecord{2935d010 30637:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #2: ActivityRecord{21da062c u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
ProcessRecord{2935d010 30637:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #1: ActivityRecord{3427b2a6 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity }
ProcessRecord{2935d010 30637:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #0: ActivityRecord{2524b17c u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
ProcessRecord{2935d010 30637:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Running activities (most recent first):
TaskRecord{36376d7d #610 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=5}
Run #4: ActivityRecord{10ff3848 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t610}
Run #3: ActivityRecord{d9bece2 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
Run #2: ActivityRecord{21da062c u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
Run #1: ActivityRecord{3427b2a6 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
Run #0: ActivityRecord{2524b17c u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t610}
mResumedActivity: ActivityRecord{10ff3848 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t610}
我们连续三次调用MainActivity,然后在调用TestAActivity。我们发现MainActivity不停的被创建,然后被压入一个TaskRecord。
该TaskRecord有一个标识 #610 A=org.wangchenlong.wcl_activity_launchmode_demo。
从记录中我们就能发现Standrd模式的特点就是会一直创建Activity.并且将它压入启动他的Activity所在的TaskRecord中。
四,SingleTop(栈顶复用模式)
解释:只有当Activity位于栈(TaskRecord)顶的时候,再次启动当前的Activity,复用栈顶的Activity实例。不会重复创建。如果位于栈(TaskRecord)内(中间位置),则与标准模式相同,仍然会重新创建实例。
示例:
MainActivity -> TestAActivity -> TestBActivity -> TestBActivity -> TestBActivity。其中TestBActivity是SingleTop模式。
adb记录如下:
Stack #1:
Task id #614
TaskRecord{cf6619 #614 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=3}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
Hist #2: ActivityRecord{38dea612 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t614}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
ProcessRecord{21ad473a 8067:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #1: ActivityRecord{34c729dc u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t614}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
ProcessRecord{21ad473a 8067:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #0: ActivityRecord{109cfbb9 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t614}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
ProcessRecord{21ad473a 8067:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Running activities (most recent first):
TaskRecord{cf6619 #614 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=3}
Run #2: ActivityRecord{38dea612 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t614}
Run #1: ActivityRecord{34c729dc u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t614}
Run #0: ActivityRecord{109cfbb9 u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t614}
mResumedActivity: ActivityRecord{38dea612 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t614}
从记录中我们可以发现,和Standrd模式同样的操作,但是SingleTop模式的TestBActivity却没有重复创建Activity实例,正如上面解释中所描述的那样,这也是SingleTop模式的特点。
示例:
MainActivity -> TestAActivity -> TestBActivity -> TestCActivity -> TestBActivity -> TestBActivity。其中TestBActivity是SingleTop模式。
adb记录如下:
Stack #1:
Task id #615
TaskRecord{fa6b5df #615 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=5}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
Hist #4: ActivityRecord{259bbae0 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t615}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
ProcessRecord{21ad473a 8067:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #3: ActivityRecord{2d3af4ba u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t615}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity }
ProcessRecord{21ad473a 8067:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #2: ActivityRecord{1fbc1944 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t615}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity }
ProcessRecord{21ad473a 8067:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #1: ActivityRecord{1645b2b9 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t615}
Intent { cmp=org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity }
ProcessRecord{21ad473a 8067:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Hist #0: ActivityRecord{17ae047f u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t615}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity (has extras) }
ProcessRecord{21ad473a 8067:org.wangchenlong.wcl_activity_launchmode_demo/u0a460}
Running activities (most recent first):
TaskRecord{fa6b5df #615 A=org.wangchenlong.wcl_activity_launchmode_demo U=0 sz=5}
Run #4: ActivityRecord{259bbae0 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t615}
Run #3: ActivityRecord{2d3af4ba u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestCActivity t615}
Run #2: ActivityRecord{1fbc1944 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t615}
Run #1: ActivityRecord{1645b2b9 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestAActivity t615}
Run #0: ActivityRecord{17ae047f u0 org.wangchenlong.wcl_activity_launchmode_demo/.MainActivity t615}
mResumedActivity: ActivityRecord{259bbae0 u0 org.wangchenlong.wcl_activity_launchmode_demo/.TestBActivity t615}
从上面的记录中我们再次验证了解释中的,SingleTop模式的特点,请大家牢记。
Demo下载:https://download.csdn.net/download/wangxp423/10767068
参考:《高级Android开发强化实战》
推荐:https://github.com/wangxp423 喜欢的小伙伴点一下 Star