Android入门之Activity四种启动模式
一、启动模式介绍
启动模式简单地说就是Activity启动时的策略,在AndroidManifest.xml中的<Activity>标签的android:launchMode属性设置;
启动模式有4种,分别为standard、singleTop、singleTask、singleInstance;
讲解启动模式之前,有必要先讲解一下“任务栈”的概念;
任务栈
每个应用都有一个任务栈,是用来存放Activity的,功能类似于函数调用的栈,先后顺序代表了Activity的出现顺序;比如Activity1-->Activity2-->Activity3,则任务栈为:
启动模式
(1)standard:每次激活Activity时(startActivity),都创建Activity实例,并放入任务栈;【达内3G培训】
(2)singleTop:如果某个Activity自己激活自己,即任务栈栈顶就是该Activity,则不需要创建,其余情况都要创建Activity实例;
(3)singleTask:如果要激活的那个Activity在任务栈中存在该实例,则不需要创建,只需要把此Activity放入栈顶,并把该Activity以上的Activity实例都pop;
(4)singleInstance:如果应用1的任务栈中创建了MainActivity实例,如果应用2也要激活MainActivity,则不需要创建,两应用共享该Activity实例;【达内3G培训】
二、代码实例
1.standard
场景描述:MainActivity点击按钮激活自己,但是MainActivity的引用发生改变;
效果:
![](http://www.cdtarena.com/Upfiles/Article/2012072610281195726.png)
AndroidManifest.xml
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" android:launchMode="standard">
MainActivity.java
- package com.xiazdong.launchmode;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- publicclass MainActivity extends Activity {
- private Button standardButton;
- private TextView showView;
- private OnClickListener listener = new OnClickListener(){
- @Override
- publicvoid onClick(View v) {
- Intent intent = new Intent(MainActivity.this,MainActivity.class);
- startActivity(intent);
- }
- };
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- showView = (TextView)this.findViewById(R.id.showview);
- standardButton = (Button)this.findViewById(R.id.standard);
- standardButton.setOnClickListener(listener);
- showView.setText(this.toString());
- }
- }
2.singleTop
场景描述:MainActivity自己激活自己,但是MainActivity并没有创建实例,即引用不变;
效果:【达内3G培训】
![](http://www.cdtarena.com/Upfiles/Article/2012072610281213988.png)
AndroidManifest.xml
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name"android:launchMode="singleTop">
MainActivity.java
- package com.xiazdong.launchmode;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- publicclass MainActivity extends Activity {
- private Button standardButton;
- private TextView showView;
- private OnClickListener listener = new OnClickListener(){
- @Override
- publicvoid onClick(View v) {
- Intent intent = new Intent(MainActivity.this,MainActivity.class);
- startActivity(intent);
- }
- };
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- showView = (TextView)this.findViewById(R.id.showview);
- standardButton = (Button)this.findViewById(R.id.standard);
- standardButton.setOnClickListener(listener);
- showView.setText(this.toString());
- }
- }
3.singleTask
场景描述:
MainActivity
的启动模式为singleTask,MainActivity激活OtherActivity,然后OtherActivity激活
MainActivity,发现MainActivity引用不变,然后MainActivity再次激活OtherActivity,但是
OtherActivity的引用变了;
效果:
![](http://www.cdtarena.com/Upfiles/Article/2012072610281372467.png)
AndroidManifest.xml
- <activity
- android:name=".MainActivity"
- android:label="MainActivity"android:launchMode="singleTask">
- </activity>
- <activity
- android:name=".OtherActivity"android:launchMode="singleTask"android:label="OtherActivity"
- ></activity>
MainActivity和OtherActivity就是简单的代码,略;
4.singleInstance
场景描述:App1应用和App2应用分别激活SingleInstance应用的MainActivity,发现两个应用激活后MainActivity的引用一样;
效果:
![](http://www.cdtarena.com/Upfiles/Article/2012072610281561601.png)
SingleInstance的AndroidManifest.xml
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name"android:launchMode="singleInstance">