activity四种启动模式

activity的四种启动模式:

  1. standard:默认模式,activity在每次启动的时候都会有一个新的实例加入到task中。
  2. singleTop:如果在顶部已经存在一个activity的实例,系统就会通过调用onNewIntent()方法来启动,无需再此实例化一个activity。
  3. singleTask:系统会创建一个新的task和一个activity实例在task的根部,如果activity已经存在单独的task中,系统就会调用onNewIntent() 方法类,而不是进行实例化一个新的实例,仅有一个activity实例同时存在。
  4. singleInstance:和singleTask相似,除了系统不会让其他的activities运行在所有持有的task实例中,这个activity是独立的,并且task中的成员只有它,任何其他activities运行这个activity都将打开一个独立的task。

四种启动模式的的设置方式

  1. 在manifest文件中设置,例如
     <activity android:name=".ActivityA" android:launchMode="standard">
     红色字体部分就是设置启动模式为标准模式,当然,默认就是标准模式。
  2. 通过Intent flags设置,也就是在代码中设置。在代码中设置包括三种:
  • FLAG_ACTIVITY_NEW_TASK:同singleTask。
  • FLAG_ACTIVITY_SINGLE_TOP:  同singleTop。
  • FLAG_ACTIVITY_CLEAR_TOP :If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent()).在四种启动模式中没有对应的值。

       使用方法就是在启动actvity的intent中设置如下

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

猜你喜欢

转载自deep-fish.iteye.com/blog/2058877