当前位置:   article > 正文

【Activity】Activity的启动模式:SingleTop

singletop

目录

1. 概述

2. 场景1:MainActivity->SecondActivity->SecondActivity

2.1 启动MainActivity

2.1.1 生命周期

2.1.2 Activity栈

2.2 MainActivity中启动SecondActivity

2.2.1 生命周期

2.2.2 Activity栈

2.3 SecondActivity中启动SecondActivity

2.3.1 生命周期

2.3.2 Activity栈

3.场景2:MainActivity->SecondActivity->MainActivity->SecondActivity

3.1 启动MainActivity

3.1.1 生命周期

3.1.2 Activity栈

3.2 MainActivity中启动SecondActivity

3.2.1 生命周期

3.2.2 Activity栈

3.3 SecondActivity中启动MainActivity

3.3.1 生命周期

3.3.2 Activity栈

3.4 MainActivity中启动SecondActivity

3.4.1 生命周期

3.4.2 Activity栈

4.总结


1. 概述

官方文档对“singleTop”启动模式的说明:

“如果目标任务在其堆栈顶部已有一个 Activity 实例,那么该实例将接收新 Intent(通过调用 onNewIntent());此时不会创建新实例。在其他情况下 — 例如,如果“singleTop”的一个现有实例虽在目标任务内,但未处于堆栈顶部,或者虽然位于堆栈顶部,但不在目标任务中 — 则系统会创建一个新实例并将其推送到堆栈上。”

接着就用Demo来验证一下。

Demo中的launchMode:

  1. MainActivity-Standard
  2. SecondActivity-SingleTop

2. 场景1:MainActivity->SecondActivity->SecondActivity

2.1 启动MainActivity

2.1.1 生命周期

  1. 2019-02-25 09:00:21.765 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onCreate:this=com.maureen.testtwoactivity.MainActivity@f6993c1
  2. 2019-02-25 09:00:21.897 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStart:this=com.maureen.testtwoactivity.MainActivity@f6993c1
  3. 2019-02-25 09:00:21.900 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onResume:this=com.maureen.testtwoactivity.MainActivity@f6993c1

2.1.2 Activity栈

  1. TaskRecord{4c3267a #37 A=com.maureen.testtwoactivity U=0 StackId=6 sz=1}
  2. Run #0: ActivityRecord{bb2ee2f u0 com.maureen.testtwoactivity/.MainActivity t37}

2.2 MainActivity中启动SecondActivity

2.2.1 生命周期

  1. 2019-02-25 09:00:59.625 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onPause:this=com.maureen.testtwoactivity.MainActivity@f6993c1
  2. 2019-02-25 09:00:59.690 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onCreate:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
  3. 2019-02-25 09:00:59.821 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStart:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
  4. 2019-02-25 09:00:59.825 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
  5. 2019-02-25 09:01:00.570 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStop:this=com.maureen.testtwoactivity.MainActivity@f6993c1
  6. 2019-02-25 09:01:00.573 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.MainActivity@f6993c1

2.2.2 Activity栈

  1. TaskRecord{4c3267a #37 A=com.maureen.testtwoactivity U=0 StackId=6 sz=2}
  2. Run #1: ActivityRecord{ed1f421 u0 com.maureen.testtwoactivity/.SecondActivity t37}
  3. Run #0: ActivityRecord{bb2ee2f u0 com.maureen.testtwoactivity/.MainActivity t37}

2.3 SecondActivity中启动SecondActivity

2.3.1 生命周期

  1. 2019-02-25 09:01:33.386 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onPause:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
  2. 2019-02-25 09:01:33.387 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onNewIntent:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9
  3. 2019-02-25 09:01:33.390 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@6c8c7b9

2.3.2 Activity栈

  1. TaskRecord{4c3267a #37 A=com.maureen.testtwoactivity U=0 StackId=6 sz=2}
  2. Run #1: ActivityRecord{ed1f421 u0 com.maureen.testtwoactivity/.SecondActivity t37}
  3. Run #0: ActivityRecord{bb2ee2f u0 com.maureen.testtwoactivity/.MainActivity t37}

3.场景2:MainActivity->SecondActivity->MainActivity->SecondActivity

3.1 启动MainActivity

3.1.1 生命周期

  1. 2019-02-25 09:05:37.417 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onCreate:this=com.maureen.testtwoactivity.MainActivity@a20eef8
  2. 2019-02-25 09:05:37.599 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStart:this=com.maureen.testtwoactivity.MainActivity@a20eef8
  3. 2019-02-25 09:05:37.603 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onResume:this=com.maureen.testtwoactivity.MainActivity@a20eef8

3.1.2 Activity栈

  1. TaskRecord{90f809a #38 A=com.maureen.testtwoactivity U=0 StackId=7 sz=1}
  2. Run #0: ActivityRecord{ebfb748 u0 com.maureen.testtwoactivity/.MainActivity t38}

3.2 MainActivity中启动SecondActivity

3.2.1 生命周期

  1. 2019-02-25 09:06:32.960 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onPause:this=com.maureen.testtwoactivity.MainActivity@a20eef8
  2. 2019-02-25 09:06:33.055 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onCreate:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
  3. 2019-02-25 09:06:33.182 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStart:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
  4. 2019-02-25 09:06:33.185 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
  5. 2019-02-25 09:06:33.914 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStop:this=com.maureen.testtwoactivity.MainActivity@a20eef8
  6. 2019-02-25 09:06:33.916 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.MainActivity@a20eef

3.2.2 Activity栈

  1. TaskRecord{90f809a #38 A=com.maureen.testtwoactivity U=0 StackId=7 sz=2}
  2. Run #1: ActivityRecord{c9d2cc1 u0 com.maureen.testtwoactivity/.SecondActivity t38}
  3. Run #0: ActivityRecord{ebfb748 u0 com.maureen.testtwoactivity/.MainActivity t38}

3.3 SecondActivity中启动MainActivity

3.3.1 生命周期

  1. 2019-02-25 09:07:16.659 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onPause:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
  2. 2019-02-25 09:07:16.750 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onCreate:this=com.maureen.testtwoactivity.MainActivity@84a0e18
  3. 2019-02-25 09:07:16.861 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStart:this=com.maureen.testtwoactivity.MainActivity@84a0e18
  4. 2019-02-25 09:07:16.864 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onResume:this=com.maureen.testtwoactivity.MainActivity@84a0e18
  5. 2019-02-25 09:07:17.591 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStop:this=com.maureen.testtwoactivity.SecondActivity@a7068ff
  6. 2019-02-25 09:07:17.593 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.SecondActivity@a7068ff

3.3.2 Activity栈

  1. TaskRecord{90f809a #38 A=com.maureen.testtwoactivity U=0 StackId=7 sz=3}
  2. Run #2: ActivityRecord{1219cd8 u0 com.maureen.testtwoactivity/.MainActivity t38}
  3. Run #1: ActivityRecord{c9d2cc1 u0 com.maureen.testtwoactivity/.SecondActivity t38}
  4. Run #0: ActivityRecord{ebfb748 u0 com.maureen.testtwoactivity/.MainActivity t38}

3.4 MainActivity中启动SecondActivity

3.4.1 生命周期

  1. 2019-02-25 09:08:13.364 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onPause:this=com.maureen.testtwoactivity.MainActivity@84a0e18
  2. 2019-02-25 09:08:13.416 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onCreate:this=com.maureen.testtwoactivity.SecondActivity@e5b1142
  3. 2019-02-25 09:08:13.519 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStart:this=com.maureen.testtwoactivity.SecondActivity@e5b1142
  4. 2019-02-25 09:08:13.522 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@e5b1142
  5. 2019-02-25 09:08:14.244 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStop:this=com.maureen.testtwoactivity.MainActivity@84a0e18
  6. 2019-02-25 09:08:14.246 3508-3508/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.MainActivity@84a0e18

3.4.2 Activity栈

  1. TaskRecord{90f809a #38 A=com.maureen.testtwoactivity U=0 StackId=7 sz=4}
  2. Run #3: ActivityRecord{14d1dab u0 com.maureen.testtwoactivity/.SecondActivity t38} //在栈顶,栈中包含两个SecondActivity实例
  3. Run #2: ActivityRecord{1219cd8 u0 com.maureen.testtwoactivity/.MainActivity t38}
  4. Run #1: ActivityRecord{c9d2cc1 u0 com.maureen.testtwoactivity/.SecondActivity t38}
  5. Run #0: ActivityRecord{ebfb748 u0 com.maureen.testtwoactivity/.MainActivity t38}

4.总结

就如同官方文档说明的,以“singleTop”模式启动的Activity:

  • 如果目标任务中已经存在该Activity实例,且该实例位于栈顶,则调用该实例的onNewIntent(),而不是创建新的实例(见场景1);
  • 如果目标任务中已经存在该Activity实例,但是该实例不是位于栈顶,则创建新的实例(见场景2);
  • 如果已经有该Activity实例位于栈顶,但是该任务不是目标任务,也要创建一个实例且将其推至目标任务的栈顶(暂未找到合适的例子)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/编程挑战者/article/detail/60723
推荐阅读
相关标签
  

闽ICP备14008679号