赞
踩
目录
2. 场景1:MainActivity->SecondActivity->SecondActivity
2.2 MainActivity中启动SecondActivity
2.3 SecondActivity中启动SecondActivity
3.场景2:MainActivity->SecondActivity->MainActivity->SecondActivity
3.2 MainActivity中启动SecondActivity
3.3 SecondActivity中启动MainActivity
3.4 MainActivity中启动SecondActivity
官方文档对“singleTop”启动模式的说明:
“如果目标任务在其堆栈顶部已有一个 Activity 实例,那么该实例将接收新 Intent(通过调用 onNewIntent()
);此时不会创建新实例。在其他情况下 — 例如,如果“singleTop
”的一个现有实例虽在目标任务内,但未处于堆栈顶部,或者虽然位于堆栈顶部,但不在目标任务中 — 则系统会创建一个新实例并将其推送到堆栈上。”
接着就用Demo来验证一下。
Demo中的launchMode:
- MainActivity-Standard
- SecondActivity-SingleTop
- 2019-02-25 09:00:21.765 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onCreate:this=com.maureen.testtwoactivity.MainActivity@f6993c1
- 2019-02-25 09:00:21.897 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStart:this=com.maureen.testtwoactivity.MainActivity@f6993c1
- 2019-02-25 09:00:21.900 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onResume:this=com.maureen.testtwoactivity.MainActivity@f6993c1
- TaskRecord{4c3267a #37 A=com.maureen.testtwoactivity U=0 StackId=6 sz=1}
- Run #0: ActivityRecord{bb2ee2f u0 com.maureen.testtwoactivity/.MainActivity t37}
- 2019-02-25 09:00:59.625 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onPause:this=com.maureen.testtwoactivity.MainActivity@f6993c1
- 2019-02-25 09:00:59.690 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onCreate:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
- 2019-02-25 09:00:59.821 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStart:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
- 2019-02-25 09:00:59.825 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
- 2019-02-25 09:01:00.570 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStop:this=com.maureen.testtwoactivity.MainActivity@f6993c1
- 2019-02-25 09:01:00.573 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.MainActivity@f6993c1
- TaskRecord{4c3267a #37 A=com.maureen.testtwoactivity U=0 StackId=6 sz=2}
- Run #1: ActivityRecord{ed1f421 u0 com.maureen.testtwoactivity/.SecondActivity t37}
- Run #0: ActivityRecord{bb2ee2f u0 com.maureen.testtwoactivity/.MainActivity t37}
- 2019-02-25 09:01:33.386 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onPause:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
- 2019-02-25 09:01:33.387 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onNewIntent:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
- 2019-02-25 09:01:33.390 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
- TaskRecord{4c3267a #37 A=com.maureen.testtwoactivity U=0 StackId=6 sz=2}
- Run #1: ActivityRecord{ed1f421 u0 com.maureen.testtwoactivity/.SecondActivity t37}
- Run #0: ActivityRecord{bb2ee2f u0 com.maureen.testtwoactivity/.MainActivity t37}
- 2019-02-25 09:05:37.417 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onCreate:this=com.maureen.testtwoactivity.MainActivity@a20eef8
- 2019-02-25 09:05:37.599 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStart:this=com.maureen.testtwoactivity.MainActivity@a20eef8
- 2019-02-25 09:05:37.603 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onResume:this=com.maureen.testtwoactivity.MainActivity@a20eef8
- TaskRecord{90f809a #38 A=com.maureen.testtwoactivity U=0 StackId=7 sz=1}
- Run #0: ActivityRecord{ebfb748 u0 com.maureen.testtwoactivity/.MainActivity t38}
- 2019-02-25 09:06:32.960 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onPause:this=com.maureen.testtwoactivity.MainActivity@a20eef8
- 2019-02-25 09:06:33.055 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onCreate:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
- 2019-02-25 09:06:33.182 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStart:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
- 2019-02-25 09:06:33.185 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
- 2019-02-25 09:06:33.914 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStop:this=com.maureen.testtwoactivity.MainActivity@a20eef8
- 2019-02-25 09:06:33.916 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.MainActivity@a20eef
- TaskRecord{90f809a #38 A=com.maureen.testtwoactivity U=0 StackId=7 sz=2}
- Run #1: ActivityRecord{c9d2cc1 u0 com.maureen.testtwoactivity/.SecondActivity t38}
- Run #0: ActivityRecord{ebfb748 u0 com.maureen.testtwoactivity/.MainActivity t38}
- 2019-02-25 09:07:16.659 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onPause:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
- 2019-02-25 09:07:16.750 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onCreate:this=com.maureen.testtwoactivity.MainActivity@84a0e18
- 2019-02-25 09:07:16.861 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStart:this=com.maureen.testtwoactivity.MainActivity@84a0e18
- 2019-02-25 09:07:16.864 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onResume:this=com.maureen.testtwoactivity.MainActivity@84a0e18
- 2019-02-25 09:07:17.591 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStop:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
- 2019-02-25 09:07:17.593 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
- TaskRecord{90f809a #38 A=com.maureen.testtwoactivity U=0 StackId=7 sz=3}
- Run #2: ActivityRecord{1219cd8 u0 com.maureen.testtwoactivity/.MainActivity t38}
- Run #1: ActivityRecord{c9d2cc1 u0 com.maureen.testtwoactivity/.SecondActivity t38}
- Run #0: ActivityRecord{ebfb748 u0 com.maureen.testtwoactivity/.MainActivity t38}
- 2019-02-25 09:08:13.364 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onPause:this=com.maureen.testtwoactivity.MainActivity@84a0e18
- 2019-02-25 09:08:13.416 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onCreate:this=com.maureen.testtwoactivity.SecondActivity@e5b1142
- 2019-02-25 09:08:13.519 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStart:this=com.maureen.testtwoactivity.SecondActivity@e5b1142
- 2019-02-25 09:08:13.522 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@e5b1142
- 2019-02-25 09:08:14.244 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStop:this=com.maureen.testtwoactivity.MainActivity@84a0e18
- 2019-02-25 09:08:14.246 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.MainActivity@84a0e18
- TaskRecord{90f809a #38 A=com.maureen.testtwoactivity U=0 StackId=7 sz=4}
- Run #3: ActivityRecord{14d1dab u0 com.maureen.testtwoactivity/.SecondActivity t38} //在栈顶,栈中包含两个SecondActivity实例
- Run #2: ActivityRecord{1219cd8 u0 com.maureen.testtwoactivity/.MainActivity t38}
- Run #1: ActivityRecord{c9d2cc1 u0 com.maureen.testtwoactivity/.SecondActivity t38}
- Run #0: ActivityRecord{ebfb748 u0 com.maureen.testtwoactivity/.MainActivity t38}
就如同官方文档说明的,以“singleTop”模式启动的Activity:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。