Android Activity launchMode singleTask/singleTop/singleInstance/standard 讲解

先写一个基础的Activity实现日志打印,将各个生命周期的方法都打印出来。

public class BaseActivity extends Activity{

    public static final String tag ="BaseActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(tag,"onCreate "+getComponentName().getShortClassName());
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(tag,"onNewIntent "+getComponentName().getShortClassName());
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(tag,"onStart "+getComponentName().getShortClassName());
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(tag,"onRestart "+getComponentName().getShortClassName());
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(tag,"onResume "+getComponentName().getShortClassName());
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(tag,"onPause "+getComponentName().getShortClassName());
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(tag,"onStop "+getComponentName().getShortClassName());
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(tag,"onDestroy "+getComponentName().getShortClassName());
    }
}

2.实现多个Activity 注册mainifreset

<activity android:name=".Main3Activity"
      android:launchMode="singleTask">
      <intent-filter>
          <action android:name="android.intent.action.MAIN"/>

          <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
  </activity>
  <activity android:name=".Main4Activity">
  </activity>
  <activity android:name=".Main5Activity"
      android:process="">
  </activity>

测试单个Activity 生命周期

11-21 14:11:43.586 17004-17004/application D/BaseActivity: onCreate .Main3Activity
11-21 14:11:43.601 17004-17004/application D/BaseActivity: onStart .Main3Activity
11-21 14:11:43.602 17004-17004/application D/BaseActivity: onResume .Main3Activity
11-21 14:11:45.631 17004-17004/application D/BaseActivity: onPause .Main3Activity
11-21 14:11:46.183 17004-17004/application D/BaseActivity: onStop .Main3Activity
11-21 14:11:46.183 17004-17004/application D/BaseActivity: onDestroy .Main3Activity

测试2个Activity跳转时的(Standard模式)

11-21 14:12:54.390 17004-17004/application D/BaseActivity: onCreate .Main3Activity
11-21 14:12:54.414 17004-17004/application D/BaseActivity: onStart .Main3Activity
11-21 14:12:54.415 17004-17004/application D/BaseActivity: onResume .Main3Activity
11-21 14:12:56.241 17004-17004/application D/BaseActivity: onPause .Main3Activity
11-21 14:12:56.253 17004-17004/application D/BaseActivity: onCreate .Main4Activity
11-21 14:12:56.264 17004-17004/application D/BaseActivity: onStart .Main4Activity
11-21 14:12:56.264 17004-17004/application D/BaseActivity: onResume .Main4Activity
11-21 14:12:56.667 17004-17004/application D/BaseActivity: onStop .Main3Activity

生命周期 分别为

onCreate ->onStart ->onResume ->onPause ->onCreate .Main4Activity->onStart .Main4Activity
->onResume .Main4Activity->onStop .Main3Activity

按返回键后 的生命周期流程:

11-21 14:15:07.981 17004-17004/application D/BaseActivity: onPause .Main4Activity
11-21 14:15:07.987 17004-17004/application D/BaseActivity: onRestart .Main3Activity
11-21 14:15:07.987 17004-17004/application D/BaseActivity: onStart .Main3Activity
11-21 14:15:07.988 17004-17004/application D/BaseActivity: onResume .Main3Activity
11-21 14:15:08.362 17004-17004/application D/BaseActivity: onStop .Main4Activity
11-21 14:15:08.362 17004-17004/application D/BaseActivity: onDestroy .Main4Activity

2.测试2个Activity跳转时的(SingleTop模式)
singleTop模式和standard模式基本相同,唯一的区别就是当task中当前activity 在top的时候不在从新创建一个新的Activity。

3.SingleTask
当前Task中包含了Main3Activity的时候 从Main4Activity 跳转进入Main3Activity的生命周期活动,
不再创建新的Activity实例,而是调用onNewIntent .Main3Activity方法 将Intent中的参数传人已经创建好的实例当中。

singleTask如果已经在task中拥有实例,再次调用将会把task中该实例的前面的所有activity全部出栈进行销毁而且不会走生命周期。

11-21 14:19:21.167 5640-5640/application D/BaseActivity: onPause .Main4Activity
11-21 14:19:21.177 5640-5640/application D/BaseActivity: onNewIntent .Main3Activity
11-21 14:19:21.177 5640-5640/application D/BaseActivity: onRestart .Main3Activity
11-21 14:19:21.178 5640-5640/application D/BaseActivity: onStart .Main3Activity
11-21 14:19:21.178 5640-5640/application D/BaseActivity: onResume .Main3Activity
11-21 14:19:21.555 5640-5640/application D/BaseActivity: onStop .Main4Activity
11-21 14:19:21.555 5640-5640/application D/BaseActivity: onDestroy .Main4Activity

4.singleInstance
singleinstance 独占一个 Task,不管是否跨进程Activity只会存在一个,如果如果已经拥有,调用后结果跟singleTask类似。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值