launch mode

转自 http://blog.csdn.net/leiswpu/article/details/6248528


Defining launch modes

Launch modes allow you to define how a new instance of an activity is associated with the current task. You can define different launch modes in two ways:

  • Using the manifest file

    When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts.

  • Using Intent flags

    When you call startActivity(), you can include a flag in the Intent that declares how (or whether) the new activity should associate with the current task.

As such, if Activity A starts Activity B, Activity B can define in its manifest how it should associate with the current task (if at all) and Activity A can also request how Activity B should associate with current task. If both activities define how Activity B should associate with a task, then Activity A's request (as defined in the intent) is honored over Activity B's request (as defined in its manifest).

Note: Some launch modes available for the manifest file are not available as flags for an intent and, likewise, some launch modes available as flags for an intent cannot be defined in the manifest.

Using the manifest file

When declaring an activity in your manifest file, you can specify how the activity should associate with a task using the <activity> element's launchMode attribute.

The launchMode attribute specifies an instruction on how the activity should be launched into a task. There are four different launch modes you can assign to the launchMode attribute:

"standard" (the default mode)
Default. The system creates a new instance of the activity in the task from which 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.
"singleTop"
If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its  onNewIntent() method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the the activity at the top of the back stack is  not an existing instance of the activity).

For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default "standard"launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is "singleTop", the existing instance of D receives the intent through onNewIntent(), because it's at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is "singleTop".

Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived inonNewIntent().

"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 the intent to the existing instance through a call to its  onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

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

"singleInstance".
Same as  "singleTask", except that the system doesn't launch any other activities into the 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.

As another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying the singleTask launch mode in the <activity> element. This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the Backbutton always takes the user to the previous activity. However, if you start an activity that specifies thesingleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. At this point, the back stack now includes all activities from the task brought forward, at the top of the stack. Figure 4 illustrates this type of scenario.

Figure 4. A representation of how an activity with launch mode "singleTask" is added to the back stack. If the activity is already a part of a background task with its own back stack, then the entire back stack also comes forward, on top of the current task.

For more information about using launch modes in the manifest file, see the <activity> element documentation, where the launchMode attribute and the accepted values are discussed more.

Note: The behaviors that you specify for your activity with the launchMode attribute can be overridden by flags included with the intent that start your activity, as discussed in the next section.

Using Intent flags

When starting an activity, you can modify the default association of an activity to its task by including flags in the intent that you deliver to startActivity(). The flags you can use to modify the default behavior are:

FLAG_ACTIVITY_NEW_TASK
Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().

This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.

FLAG_ACTIVITY_SINGLE_TOP
If the activity being started is the current activity (at the top of the back stack), then the existing instance receives a call to  onNewIntent(), instead of creating a new instance of the activity.

This produces the same behavior as the "singleTop" launchMode value, discussed in the previous section.

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() ).

There is no value for the launchMode attribute that produces this behavior.

FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.

Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard".

Handling affinities

The affinity indicates which task an activity prefers to belong to. By default, all the activities from the same application have an affinity for each other. So, by default, all activities in the same application prefer to be in the same task. However, you can modify the default affinity for an activity. Activities defined in different applications can share an affinity, or activities defined in the same application can be assigned different task affinities.

You can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.

The taskAffinity attribute takes a string value, which must be unique from the default package name declared in the <manifest> element, because the system uses that name to identify the default task affinity for the application.

The affinity comes into play in two circumstances:

  • When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.

    A new activity is, by default, launched into the task of the activity that called startActivity(). It's pushed onto the same back stack as the caller. However, if the intent passed to startActivity() contains theFLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to house the new activity. Often, it's a new task. However, it doesn't have to be. If there's already an existing task with the same affinity as the new activity, the activity is launched into that task. If not, it begins a new task.

    If this flag causes an activity to begin a new task and the user presses the Home button to leave it, there must be some way for the user to navigate back to the task. Some entities (such as the notification manager) always start activities in an external task, never as part of their own, so they always putFLAG_ACTIVITY_NEW_TASK in the intents they pass to startActivity(). If you have an activity that can be invoked by an external entity that might use this flag, take care that the user has a independent way to get back to the task that's started, such as with a launcher icon (the root activity of the task has aCATEGORY_LAUNCHER intent filter; see the Starting a task section below).

  • When an activity has its allowTaskReparenting attribute set to "true".

    In this case, the activity can move from the task it starts to the task it has an affinity for, when that task comes to the foreground.

    For example, suppose that an activity that reports weather conditions in selected cities is defined as part of a travel application. It has the same affinity as other activities in the same application (the default application affinity) and it allows re-parenting with this attribute. When one of your activities starts the weather reporter activity, it initially belongs to the same task as your activity. However, when the travel application's task comes to the foreground, the weather reporter activity is reassigned to that task and displayed within it.

Tip: If an .apk file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity attribute to assign different affinities to the activities associated with each "application".

Clearing the back stack

If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.

There are some activity attributes that you can use to modify this behavior:

alwaysRetainTaskState
If this attribute is set to  "true" in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period.
clearTaskOnLaunch
If this attribute is set to  "true" in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it. In other words, it's the opposite of alwaysRetainTaskState. The user always returns to the task in its initial state, even after a leaving the task for only a moment.
finishOnTaskLaunch
This attribute is like  clearTaskOnLaunch, but it operates on a single activity, not an entire task. It can also cause any activity to go away, including the root activity. When it's set to  "true", the activity remains part of the task only for the current session. If the user leaves and then returns to the task, it is no longer present.

Starting a task

You can set up an activity as the entry point for a task by giving it an intent filter with"android.intent.action.MAIN" as the specified action and "android.intent.category.LAUNCHER" as the specified category. For example:

<activity ... >
    <intent-filter ... >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    ...
</activity>

An intent filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way to launch the activity and to return to the task that it creates any time after it has been launched.

This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher. For this reason, the two launch modes that mark activities as always initiating a task,"singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and aCATEGORY_LAUNCHER filter. Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task. The user then presses the Home button. The task is now sent to the background and is not visible. Now the user has no way to return to the task, because it is not represented in the application launcher.

For those cases where you don't want the user to be able to return to an activity, set the <activity> element'sfinishOnTaskLaunch to "true" (see Clearing the stack).

---------------------------------------------------------------------------------------------------------

补充关于CLEAR_TOP的一个问题:

public static final int FLAG_ACTIVITY_CLEAR_TOP
Added in  API level 1

If set, and the activity being launched 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 will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not setFLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or ifFLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

See Tasks and Back Stack for more information about tasks.

Constant Value: 67108864 (0x04000000)

---------------------------------------------------------------------------------------------------------


Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance。以下逐一举例说明他们的区别:

 

standard:Activity的默认加载方法,即使某个Activity在Task栈中已经存在,另一个activity通过Intent跳转到该activity,同样会新创建一个实例压入栈中。例如:现在栈的情况为:A B C D,在D这个Activity中通过Intent跳转到D,那么现在的栈情况为: A B C D D 。此时如果栈顶的D通过Intent跳转到B,则栈情况为:A B C D D B。此时如果依次按返回键,D  D C B A将会依次弹出栈而显示在界面上。

 

singleTop:如果某个Activity的Launch mode设置成singleTop,那么当该Activity位于栈顶的时候,再通过Intent跳转到本身这个Activity,则将不会创建一个新的实例压入栈中。例如:现在栈的情况为:A B C D。D的Launch mode设置成了singleTop,那么在D中启动Intent跳转到D,那么将不会新创建一个D的实例压入栈中,此时栈的情况依然为:A B C D。但是如果此时B的模式也是singleTop,D跳转到B,那么则会新建一个B的实例压入栈中,因为此时B不是位于栈顶,此时栈的情况就变成了:A B C D B。

 

singleTask:如果某个Activity是singleTask模式,那么Task栈中将会只有一个该Activity的实例。例如:现在栈的情况为:A B C D。B的Launch mode为singleTask,此时D通过Intent跳转到B,则栈的情况变成了:A B。而C和D被弹出销毁了,也就是说位于B之上的实例都被销毁了。

关于singleTask这个网上颇有争议,包括google api上的说明也让我看的是一头雾水,自己用实例亲测,终于算是搞清楚了

正解:1.singleTask 并不一定处于栈底

   2.singleTask 并一定会是栈底的根元素 

    3.singleTask 并不一定会启动新的task  

     情况一:如果在本程序中启动singleTask的activity:假设ActivityA是程序的入口,是默认的模式(standard),ActivityB是singleTask 模式,由ActivityA启动,刚ActivityB不会位于栈底,不是根元素,不会启动新的task,此种情况ActivityB会和ActivityA在一个栈中,位于ActivityA上面

  情况二:如果ActivityB由另外一个程序启动:假设apkA是情况一中的应用,apkB是测试程序,在apkB中启动apkA中的ActivityB,刚ActivityB会位于栈底,是根元素,会启动新的task

注意singleTask模式的Activity不管是位于栈顶还是栈底,再次运行这个Activity时,都会destory掉它上面的Activity来保证整个栈中只有一个自己,切记切记

singleInstance:将Activity压入一个新建的任务栈中。例如:Task栈1的情况为:A B C。C通过Intent跳转到D,而D的Launch mode为singleInstance,则将会新建一个Task栈2。此时Task栈1的情况还是为:A B C。Task栈2的情况为:D。此时屏幕界面显示D的内容,如果这时D又通过Intent跳转到D,则Task栈2中也不会新建一个D的实例,所以两个栈的情况也不会变化。而如果D跳转到C,则栈1的情况变成了:A B C C,因为C的Launch mode为standard,此时如果再按返回键,则栈1变成:A B C。也就是说现在界面还显示C的内容,不是D。

好了,现在有一个问题就是这时这种情况下如果用户点击了Home键,则再也回不到D的即时界面了。如果想解决这个问题,可以为D在Manifest.xml文件中的声明加上:

 

<intent-filter>

       <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />

 </intent-filter>

 

加上这段之后,也就是说该程序中有两个这种声明,另一个就是那个正常的根activity,在打成apk包安装之后,在程序列表中能看到两个图标,但是如果都运行的话,在任务管理器中其实也只有一个。上面的情况点击D的那个图标就能回到它的即时界面(比如一个EditText,以前输入的内容,现在回到之后依然存在)。

 

PS:intent-filter中 <action android:name="android.intent.action.MAIN" />和 <category android:name="android.intent.category.LAUNCHER" />两个过滤条件缺一不可才会在程序列表中添加一个图标,图标下的显示文字是android:label设定的字符串。


------------------------------------------------------------------------------------------------------------------------------

在Android的联机文档中,有对Activity的简单介绍,现在通过编写代码对Activity的启动模式做一个深入的理解。 
在配置文件AndroidManifest.xml中,activity元素的android:launchMode属性用来配置对应Activity的启动模式,目前有以下四种启动模式: 
1.standard 
2.singleTop 
3.singleTask 
4.singleInstance 
如果不对Activity设置启动模式,默认就是standard模式

一、standard

请看以下代码,实现了一个Activity :
public class A_Activity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        TextView textView=new TextView(this); 
        textView.setText(this+"");//这里用于打印当前Activity的hashcode,可以此判断Activity实例是不是同一个对象 
        Button button=new Button(this); 
        button.setText("Go next activity"); 
        button.setOnClickListener(new OnClickListener(){

            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                Intent intent=new Intent(); 
                intent.setClass(A_Activity.this, A_Activity.class);//说明发出Intent与启动的Activity都是A_Activity的实例
                startActivity(intent); 
            } 
        }); 
        LinearLayout layout=new LinearLayout(this); 
        layout.setOrientation(LinearLayout.VERTICAL); 
        layout.addView(textView); 
        layout.addView(button); 
        setContentView(layout); 
    } 

运行之,请看下图: 
 
点击button后,注意看第一行的textView 
 
由此可知,生成了新的A_Activity对象,这时我们按回退键,就会依次回到之前的Activity。点击button的过程就是压栈的过程,在standard模式下,就会不断生成新的Activity对象

二、singleTop

singleTop和standard模式都会将intent发送给新的Activity实例,不同的是,如果创建Intent的时候栈顶有要创建的singleTop模式下的Activity实例,则将Intent发送给该实例,不会再创建Activity的新实例。 
还是使用之前的代码,只是设置A_Activity的启动模式为singleTop:android:launchMode="singleTop",运行之,请看下图: 
 
这个时候我们无论点击多少次button,textView都显示同一个Activity实例,按回退键时会直接退出程序,表明在singleTop模式下,如果在栈顶存在Intent中那个目标Activity的实例,就不会创建新的实例,而直接使用栈顶的对象,对于资源有限的移动设备来说,也是有实际意义的。 
如果是在不同Activity之间跳转,就会跟standard模式的情形一样,请看下面代码:
public class A_Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView=new TextView(this);
        textView.setText(this+"");
        Button button=new Button(this);
        button.setText("Go B_Activity");
        button.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent();
                intent.setClass(A_Activity.this, B_Activity.class);//从A跳转到B 
                startActivity(intent);
            }
        });
        LinearLayout layout=new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(textView);
        layout.addView(button);
        setContentView(layout);
    }
}
public class B_Activity extends Activity { 
    /**<li>Description: </li>
     *
     * @param savedInstanceState
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        TextView textView=new TextView(this);
        textView.setText(this+"");
        Button button=new Button(this);
        button.setText("Go A_Activity");
        button.setOnClickListener(new OnClickListener(){ 
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent=new Intent();
                intent.setClass(B_Activity.this, A_Activity.class);//从B跳转到A
                startActivity(intent);
            }
        });
        LinearLayout layout=new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(textView);
        layout.addView(button);
        setContentView(layout);
    }
}
运行后,如下图:
 
点击button后:
 
再点击button后:
 
这样每次都会创建目标Activity的新实例,因为在跳转时,处于栈顶的对象不是目标Activity的实例

三、singleTask

singleTask模式只能创建一个实例,当发送一个Intent,目标Activity为singleTask模式时,系统会检查栈里面是否已经有该Activity的实例,如果有就直接将Intent发送给它,还是使用(二)中的代码,将A_Activity启动模式设置为singleTask,B_Activity启动模式设置为standard,启动后如下图:
 
点击button后:
 
继续点击button:
 
由此可知,singleTask模式的A_Activity在栈中只有一个实例,可以被重复使用
并且,如果收到Intent生成一个新实例,那么用户可以通过回退键回到上一个状态,如果是已经存在的一个activity来处理这个Intent的话,就无法通过回退键回到上一个状态(对singleInstance同样适用) ,比如刚才最后一步如果再按回退键,就会直接退出程序,而不会回到上一步的状态。

四、singleInstance

这个模式下的Activity在一个单独的task栈中,这个栈也只能包含一个Activity的实例,将上面代码稍微改动一下,显示taskId:
A_Activity 中:textView.setText(this.getTaskId()+"");
B_Activity 中:textView.setText(this.getTaskId()+"");
另外将B_Activity 设置为singleInstance模式,A_Activity 设置为standard模式,启动后:
 
点击button后:
 
表明启动了新的task

总结四个模式的不同:
1、Intent的目标Activity由哪个task持有
standard与singleTop的Activity所在task,与收到的Intent的发送者所在task相同,除非Intent包括参数FLAG_ACTIVITY_NEW_TASK,该参数会启动Activity到新的task中;singleTask和singleInstance总是把Activity作为一个task的根元素,它们不会被启动到其他task里
2、是否允许Activity的多个实例
standard与singleTop可以被实例化多次,可以存在不同task中,并且一个task可以包括同一activity的多个实例
singleTask与singleInstance则在同一个task中只能允许Activity的一个实例,并且是task的根元素
3、在同一个task栈中,是否允许其他Activity的实例存在
singleInstance单独在一个task中,其他启动模式允许不同Activity的实例存在
4、是否每次生成新实例接收Intent
standard每次启动都会生成新实例
singleTop的activity如果在task的栈顶的话,则不生成新的activity实例,直接使用该实例,否则,就要生成新的实例
singleInstance在所在栈中是唯一的activity,它每次都会被重用
singleTask如果task栈中有该模式的Activity,就不生成新的activity实例,直接使用该实例,否则,就要生成新的实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值