首先,定义三个Activity,MainActivity打开SecondActivity,SecondActivity打开ThirdActivity,如下所示:
下面,我们定义Second Activity的Launch Mode,分别有下面几种情况:
1)Standard(即不定义)
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.lms.taskdemo.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.lms.taskdemo.SecondActivity">
- </activity>
- <activity android:name="com.lms.taskdemo.ThirdActivity" >
- </activity>
- </application>
- 04-21 15:23:28.373: V/com.lms.taskdemo(20473): Task : 70
- 04-21 15:23:28.373: V/com.lms.taskdemo(20473): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-21 15:23:28.373: V/com.lms.taskdemo(20473): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-21 15:23:28.373: V/com.lms.taskdemo(20473): Number of activities : 1
- 04-21 15:23:28.373: V/com.lms.taskdemo(20473): Number of running activities: 1
- 04-21 15:23:31.336: V/com.lms.taskdemo(20473): Task : 70
- 04-21 15:23:31.336: V/com.lms.taskdemo(20473): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-21 15:23:31.336: V/com.lms.taskdemo(20473): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-21 15:23:31.336: V/com.lms.taskdemo(20473): Number of activities : 2
- 04-21 15:23:31.336: V/com.lms.taskdemo(20473): Number of running activities: 2
- 04-21 15:23:33.999: V/com.lms.taskdemo(20473): Task : 70
- 04-21 15:23:33.999: V/com.lms.taskdemo(20473): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-21 15:23:33.999: V/com.lms.taskdemo(20473): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
- 04-21 15:23:33.999: V/com.lms.taskdemo(20473): Number of activities : 3
- 04-21 15:23:33.999: V/com.lms.taskdemo(20473): Number of running activities: 3
可以看到三个Activity,都是在同个Task中的。
2)SingleTask
为Second Activity,定义其launch mode 为 “singleTask”,如下:
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.lms.taskdemo.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.lms.taskdemo.SecondActivity"
- android:launchMode="singleTask">
- </activity>
- <activity android:name="com.lms.taskdemo.ThirdActivity" >
- </activity>
- </application>
再来看看其结果,如下:
- 04-22 10:31:26.197: V/com.lms.taskdemo(924): Task : 162
- 04-22 10:31:26.217: V/com.lms.taskdemo(924): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:31:26.217: V/com.lms.taskdemo(924): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:31:26.217: V/com.lms.taskdemo(924): Number of activities : 1
- 04-22 10:31:26.217: V/com.lms.taskdemo(924): Number of running activities: 1
- 04-22 10:31:31.112: V/com.lms.taskdemo(924): Task : 162
- 04-22 10:31:31.112: V/com.lms.taskdemo(924): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:31:31.112: V/com.lms.taskdemo(924): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:31:31.112: V/com.lms.taskdemo(924): Number of activities : 2
- 04-22 10:31:31.112: V/com.lms.taskdemo(924): Number of running activities: 2
- 04-22 10:31:32.884: V/com.lms.taskdemo(924): Task : 162
- 04-22 10:31:32.884: V/com.lms.taskdemo(924): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:31:32.884: V/com.lms.taskdemo(924): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
- 04-22 10:31:32.884: V/com.lms.taskdemo(924): Number of activities : 3
- 04-22 10:31:32.884: V/com.lms.taskdemo(924): Number of running activities: 2
发现虽然定义了SingleTask,但Second Activity和Main Activity还是在同一个Task中,这跟我们期望的不符,原因其实在于,设置了launchMode = SingleTask启动的Activity,在启动的时候,会去查找跟它的taskAffinity相同的task,如果存在这样一个task,就在这个task中启动,如果不存在这样一个task,才创建一个新的task,并在新task中启动。
而在这里,因为没有为各个activity定义taskAffinity,那么默认的affinity值就是包名,那么几个Activity都是一样的,所以就在同样的Task中启动了。
3)Single Task Different Task Affinity
下面,我们分别为Main Activity和 Second Activity设置不同的TaskAffinity值,如下:
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.lms.taskdemo.MainActivity"
- android:label="@string/app_name"
- android:taskAffinity="com.lms.taskdemo.MainActivity" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.lms.taskdemo.SecondActivity"
- android:launchMode="singleTask"
- android:taskAffinity="com.lms.taskdemo.SecondActivity" >
- </activity>
- <activity android:name="com.lms.taskdemo.ThirdActivity" >
- </activity>
- </application>
下面是结果,启动的顺序是Main Activity->Second Activity->Main Activity -> Second Activity -> Third Activity
- 04-22 10:26:27.818: V/com.lms.taskdemo(32677): Task : 151
- 04-22 10:26:27.818: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:26:27.818: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:26:27.818: V/com.lms.taskdemo(32677): Number of activities : 1
- 04-22 10:26:27.818: V/com.lms.taskdemo(32677): Number of running activities: 1
- 04-22 10:26:31.362: V/com.lms.taskdemo(32677): Task : 152
- 04-22 10:26:31.362: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:26:31.362: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:26:31.372: V/com.lms.taskdemo(32677): Number of activities : 1
- 04-22 10:26:31.372: V/com.lms.taskdemo(32677): Number of running activities: 1
- 04-22 10:26:36.558: V/com.lms.taskdemo(32677): Task : 151
- 04-22 10:26:36.558: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:26:36.558: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:26:36.558: V/com.lms.taskdemo(32677): Number of activities : 1
- 04-22 10:26:36.558: V/com.lms.taskdemo(32677): Number of running activities: 1
- 04-22 10:26:39.401: V/com.lms.taskdemo(32677): Task : 153
- 04-22 10:26:39.401: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:26:39.401: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:26:39.401: V/com.lms.taskdemo(32677): Number of activities : 1
- 04-22 10:26:39.401: V/com.lms.taskdemo(32677): Number of running activities: 1
- 04-22 10:26:41.123: V/com.lms.taskdemo(32677): Task : 153
- 04-22 10:26:41.123: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:26:41.123: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
- 04-22 10:26:41.123: V/com.lms.taskdemo(32677): Number of activities : 2
- 04-22 10:26:41.123: V/com.lms.taskdemo(32677): Number of running activities: 2
在上面的结果中,可以看到在启动Second Activity的时候,的确是在一个新的Task中启动了,Second Activity是这个Task中的根Activity,而由Second Activity启动的Third Activity也是在这个Task中启动的。
4)Single Instance
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.lms.taskdemo.MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.lms.taskdemo.SecondActivity"
- android:launchMode="singleInstance">
- </activity>
- <activity android:name="com.lms.taskdemo.ThirdActivity" >
- </activity>
- </application>
- 04-22 10:29:43.207: V/com.lms.taskdemo(682): Task : 159
- 04-22 10:29:43.207: V/com.lms.taskdemo(682): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:29:43.207: V/com.lms.taskdemo(682): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:29:43.207: V/com.lms.taskdemo(682): Number of activities : 1
- 04-22 10:29:43.207: V/com.lms.taskdemo(682): Number of running activities: 1
- 04-22 10:29:46.580: V/com.lms.taskdemo(682): Task : 160
- 04-22 10:29:46.580: V/com.lms.taskdemo(682): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:29:46.580: V/com.lms.taskdemo(682): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:29:46.580: V/com.lms.taskdemo(682): Number of activities : 1
- 04-22 10:29:46.580: V/com.lms.taskdemo(682): Number of running activities: 1
- 04-22 10:29:48.202: V/com.lms.taskdemo(682): Task : 159
- 04-22 10:29:48.202: V/com.lms.taskdemo(682): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:29:48.202: V/com.lms.taskdemo(682): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
- 04-22 10:29:48.202: V/com.lms.taskdemo(682): Number of activities : 2
- 04-22 10:29:48.202: V/com.lms.taskdemo(682): Number of running activities: 1
- 04-22 10:29:57.112: V/com.lms.taskdemo(682): Task : 160
- 04-22 10:29:57.112: V/com.lms.taskdemo(682): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:29:57.112: V/com.lms.taskdemo(682): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:29:57.112: V/com.lms.taskdemo(682): Number of activities : 1
- 04-22 10:29:57.112: V/com.lms.taskdemo(682): Number of running activities: 1
5)Single Instance Different Affinity
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.lms.taskdemo.MainActivity"
- android:label="@string/app_name"
- android:taskAffinity="com.lms.taskdemo.MainActivity" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name="com.lms.taskdemo.SecondActivity"
- android:launchMode="singleInstance"
- android:taskAffinity="com.lms.taskdemo.SecondActivity" >
- </activity>
- <activity android:name="com.lms.taskdemo.ThirdActivity" >
- </activity>
- </application>
下面来看看结果,如下:
- 04-22 10:27:58.866: V/com.lms.taskdemo(391): Task : 155
- 04-22 10:27:58.866: V/com.lms.taskdemo(391): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:27:58.866: V/com.lms.taskdemo(391): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
- 04-22 10:27:58.866: V/com.lms.taskdemo(391): Number of activities : 1
- 04-22 10:27:58.866: V/com.lms.taskdemo(391): Number of running activities: 1
- 04-22 10:28:00.677: V/com.lms.taskdemo(391): Task : 156
- 04-22 10:28:00.677: V/com.lms.taskdemo(391): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:28:00.677: V/com.lms.taskdemo(391): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
- 04-22 10:28:00.677: V/com.lms.taskdemo(391): Number of activities : 1
- 04-22 10:28:00.677: V/com.lms.taskdemo(391): Number of running activities: 1
- 04-22 10:28:02.309: V/com.lms.taskdemo(391): Task : 157
- 04-22 10:28:02.309: V/com.lms.taskdemo(391): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
- 04-22 10:28:02.309: V/com.lms.taskdemo(391): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
- 04-22 10:28:02.309: V/com.lms.taskdemo(391): Number of activities : 1
- 04-22 10:28:02.309: V/com.lms.taskdemo(391): Number of running activities: 1
果然,如我们期望的那样,都是在一个新的Task中启动当前的Activity的。
总结:
四种模式:
singleTop模式:可以多次实例化,但是不可以多个相同的Activity重叠,当堆栈的顶部为相同的Activity时,会调用onNewIntent函数。
singleTask模式:在使用默认taskAffinity的情况下,同一个应用中调用该Activity时,如果该Activity没有被实例化,会在本应用程序的Task内实例化,如果已经实例化,会将Task中其上的Activity销毁后,调用onNewIntent;其它应用程序调用该Activity时,如果该Activity没有被实例化,会创建新的Task并实例化后入栈,如果已经实例化,会销毁其上的Activity,并调用onNewIntent。
singleInstance模式:加载该Activity时如果没有实例化,他会创建新的Task后,实例化入栈,如果已经存在,直接调用onNewIntent,该Activity的Task中不允许启动其它的Activity,任何从该Activity启动的其他Activity都将被放到其他task中,先检查是否有本应用的task,没有的话就创建。
转载:http://blog.csdn.net/linmiansheng/article/details/24297375