Activity的四种启动模式

来源:互联网 发布:建筑工程造价软件 编辑:程序博客网 时间:2024/06/07 17:16

   刚刚重新学了一下activity的四种启动模式,现在做一下小结.

  Standard模式

 "standard" (the default mode)

  Default. The system creates a new instance of the activity in the task fromwhich it was started and routes the intent to it. The activity can be instantiated multiple times,each instance can belong to different tasks, and one task can have multiple instances.

  standard模式是activity的默认启动模式,它可以多次的被实例化,并且每一个实例可以属于不同的任务,一个任务可以实例化多次。什么意思呢,就是说当你通过一个intent去开启一个activity的时候,可以多次的开启这个页面。加入有一个按钮用来开启自身这个activity,那么每按一下这个按钮,就会启动一个activity,就是实例化一次。这些activity会在一个任务栈中进行堆叠排列起来。就像下面这样,当我们点击按钮去开启自身这个activity时,会创建一个实例,并压入栈当中。

 

   SingleTop模式

  

 "singleTop"
If an instance of the activity already exists at the top of the current task, the systemroutes the intent to that instance through a call to itsonNewIntent() method, rather than creating a new instance of theactivity. The activity can be instantiated multiple times, each instance canbelong to different tasks, and one task can have multiple instances (but only if theactivity at the top of the back stack isnot an existing instance of the activity).
   当一个Activity采用这种模式启动时,如果这个Activity已经在栈顶了,那么就不会去实例化这个activity,而是去调用onNewIntent()方法。这个activity也可以实例化多次,每个实例可以属于不同的任务并且每个任务可以实例化多次,但是前提是这个activity不处于栈顶位置。怎么理解呢,就是说,当我们去开启一个single top模式的activity时,会去检测这个activity是否处于栈顶,如果不处于栈顶,那么就会实例化一个activity,如果已经处于栈顶了,那么是不会实例化的,而是调用onNewIntent方法。比如现在任务栈里有两个activity,A和B,B采用Single Top模式,并且现在处于栈顶,当再去开启B时,是没有反应的,因为它已经处于栈顶位置,是不会实例化的,如果去开启A,那么此时又有一个A被实例化,且处于B的上面,此时再去开启B就会实例化这个Bactivity。

 

"singleTask"
The system creates a new task and instantiates the activity at the root of the new task.However, if an instance of the activity already exists in a separate task, the system routes theintent to the existing instance through a call to itsonNewIntent() method, rather than creating a new instance. Onlyone instance of the activity can exist at a time.

Note: Although the activity starts in a new task, theBack button still returns the user to the previous activity.

        当一个Activity采用Singleask模式启动时,这个Activity只能呢过被实例化一次,也就是说一个任务栈中只能存在这个Activity一次,已达到服用的作用。就拿上面的A,B 这两个activity来说,如果此时A在B的上面,如果再去开启B的话,他会检测任务栈中是否已存在,如果没有,则重新创建一个,但此时任务栈中已经有了它的实例,只是它在A的下面,那么怎么办呢?这时会清空它上面的所有activity实例,以让B这个activity处于栈顶位置,此时也是去调用onNewIntent()方法。

 "singleInstance"

 Same as"singleTask", except that the system doesn't launch any other activities intothe task holding the instance. The activity is always the single and only member of its task;any activities started by this one open in a separate task.

 采用singleInstance模式启动一个activity时,这个activity将会单独在一个任务栈中,并且只有一个实例,整个操作系统中也只有这么一个实例的activity存在于属于它自己的任务栈中。什么意思呢,就是说当我去开启这种模式的activity时,将会在一个单独的任务栈中去实例化它,并且只能实例化一次。比如呼叫来电界面,它只能实例化一次,如果可以实例化多次,当有另一个电话打进来时就会又开启一个呼叫来电界面。


 


0 0