Android中Activity启动模式探索

时间:2020-12-09 22:17:20

Android中启动模式(launchMode)分为standard, singleTop, singleTask, singleInstance四种,可通过AndroidManifest.xml文件设置某个activity的启动模式。接下来会一一探索启动模式对app行为的影响。用到的app和activity构成图如下,每次改变launchMode都只针对App2的SecondActivity:

Android中Activity启动模式探索

Standard

  1. App2内部跳转
    从Main到Second再到Third再到Second,可以看到日志记录如下:
    Android中Activity启动模式探索

  2. App1跳转到App2
    保持App2当前状态切到后台,运行App1从App1:Main跳转到App2:Second跳转到App2:Third,log记录如下:
    Android中Activity启动模式探索
    Android中Activity启动模式探索

可以看出,当采用standard作为启动模式时(standard也是Activity的缺省启动模式),每次启动该Activity都会产生一个新的实例,App内的Activity处于同一任务栈中;不同App间Activity调起时,被调起者与调起者也处于同一任务栈,当然,ApplicationContext是不一样的。

singleTop

  1. App2内部跳转
    从Main到Second再到Third再到Second,可以看到日志记录如下:
    Android中Activity启动模式探索

  2. App1跳转到App2
    保持App2最顶端为SecondActivity切到后台,运行App1从App1:Main跳转到App2:Second跳转到App2:Third,log记录如下:
    Android中Activity启动模式探索
    Android中Activity启动模式探索
  3. 暂时修改App2:Second的跳转逻辑为Second->Second,即自己跳自己,log记录如下:
    Android中Activity启动模式探索

可以看出,当采用singleTop作为启动模式时,行为特性和standard基本一致,唯一不同的情形是,singleTop的Activity位于后退栈顶、且该任务栈正与用户交互时,当有新的Intent要打开这个Activity不再产生一个新的实例,而是使用调栈实例同时回调栈顶实例的onNewIntent()方法将新的Intent传入。

顺便附上doc说明:
If, when starting the activity, there is already an instance of the same activity class in the foreground that is interacting with the user, then re-use that instance. This existing instance will receive a call to {@link android.app.Activity#onNewIntent Activity.onNewIntent()} with the new Intent that is being started.

singleTask

  1. App2内部跳转
    从Main到Second再到Third再到Second,可以看到日志记录如下:
    Android中Activity启动模式探索

  2. App1跳转到App2
    保持App2最顶端为ThirdActivity切到后台,运行App1从App1:Main跳转到App2:Second跳转到App2:Third,log记录如下:
    Android中Activity启动模式探索
    Android中Activity启动模式探索

可以看出,当采用singleTask作为启动模式时,行为特性和singleTop基本一致,唯一不同的情形是,使用旧Activity实例的情况不再局限于“该位于后退栈顶、且该任务栈正与用户交互时”,而是只要某个Task中已存在该Activity实例,就会调起该后台运行Task,意味着调起方Task与被调起方Task并不处于同一后退堆栈

顺便附上doc说明:
If, when starting the activity, there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to the front. The existing instance will receive a call to {@link android.app.Activity#onNewIntent Activity.onNewIntent()} with the new Intent that is being started, and with the {@link android.content.Intent#FLAG_ACTIVITY_BROUGHT_TO_FRONT Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT} flag set. This is a superset of the singleTop mode, where if there is already an instance of the activity being started at the top of the stack, it will receive the Intent as described there (without the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set). See the Tasks and Back Stack document for more details about tasks.

singleInstance

  1. App2内部跳转
    从Main到Second再到Third再到Second,可以看到日志记录如下:
    Android中Activity启动模式探索
    可以看到,页面扭转时SecondActivity独自拥有了一个任务栈,跳到Third后Third的任务栈和Main维持一致,所以这里,我们的App2内就存在着2个任务栈。猜想一下,这个时候我们的回退栈是什么样的?出乎意料,后退时,会先以任务栈为单位后退,当前一个任务栈清空后,再进入后一个任务栈的顶端逐一移除

  2. App1跳转到App2
    保持App2最顶端为ThirdActivity切到后台,运行App1从App1:Main跳转到App2:Second跳转到App2:Third,log记录如下:
    Android中Activity启动模式探索
    Android中Activity启动模式探索

可以看出,当采用singleInstance作为启动模式时,行为特性最特殊,被singleInstance标记的Activity会单独占有一个任务,如果已有一个Activity实例存在则不会再实例化新实例,而是把原实例所在任务唤醒到前台并回调其onNewIntent方法。

顺便附上doc说明:
Only allow one instance of this activity to ever be running. This activity gets a unique task with only itself running in it; if it is ever launched again with the same Intent, then that task will be brought forward and its {@link android.app.Activity#onNewIntent Activity.onNewIntent()} method called. If this activity tries to start a new activity, that new activity will be launched in a separate task. See the Tasks and Back Stack document for more details about tasks.