先看一下singleInstance启动模式的说明:

只有一个实例,并且这个实例独立运行在一个activity任务栈中,这个task只有这个实例,不允许有别的Activity存在下面我们通过一个实例来了解singleInstance启动模式

在手机上的运行结果如下图

android:Activity启动模式之singleInstance_ide

layout布局文件为


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity1" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity2" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity3" />

</LinearLayout>


分别为三个按钮添加点击事件,点击后分别打开对应的Activity.三个Acitivy大同小异。很容易区分,只不过Activity2是singleInstance启动模式 其它两个为默认的启动模式(standard)


MainActivity(Activity1).class


package com.example.activitysingleinstance;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
private Button btn_open1,btn_open2,btn_open3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Activity1");
//在日志中输出栈ID
Log.d("SingleInstance", "Activity1的栈ID:"+getTaskId()+"");
btn_open1=(Button) findViewById(R.id.button1);
btn_open2=(Button) findViewById(R.id.button2);
btn_open3=(Button) findViewById(R.id.button3);
btn_open1.setOnClickListener(this);
btn_open2.setOnClickListener(this);
btn_open3.setOnClickListener(this);

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
startActivity(new Intent(this,MainActivity.class));
break;
case R.id.button2:
startActivity(new Intent(this,Activity2.class));
break;
case R.id.button3:
startActivity(new Intent(this,Activity3.class));
break;
}
}

}


Activity2.class


package com.example.activitysingleinstance;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Activity2 extends Activity implements OnClickListener{
private Button btn_open1,btn_open2,btn_open3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Activity2");
//在日志中输出栈ID
Log.d("SingleInstance", "Activity2的栈ID:"+getTaskId()+"");
btn_open1=(Button) findViewById(R.id.button1);
btn_open2=(Button) findViewById(R.id.button2);
btn_open3=(Button) findViewById(R.id.button3);
btn_open1.setOnClickListener(this);
btn_open2.setOnClickListener(this);
btn_open3.setOnClickListener(this);

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
startActivity(new Intent(this,MainActivity.class));
break;
case R.id.button2:
startActivity(new Intent(this,Activity2.class));
break;
case R.id.button3:
startActivity(new Intent(this,Activity3.class));
break;
}
}
//如果调用这个方法就在日志中输出信息
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
Log.d("SingleInstance", "Activity2调用onNewIntent");
}

}


Activity3.class


package com.example.activitysingleinstance;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Activity3 extends Activity implements OnClickListener{
private Button btn_open1,btn_open2,btn_open3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Activity3");
//在日志中输出栈ID
Log.d("SingleInstance", "Activity3的栈ID:"+getTaskId()+"");
btn_open1=(Button) findViewById(R.id.button1);
btn_open2=(Button) findViewById(R.id.button2);
btn_open3=(Button) findViewById(R.id.button3);
btn_open1.setOnClickListener(this);
btn_open2.setOnClickListener(this);
btn_open3.setOnClickListener(this);

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
startActivity(new Intent(this,MainActivity.class));
break;
case R.id.button2:
startActivity(new Intent(this,Activity2.class));
break;
case R.id.button3:
startActivity(new Intent(this,Activity3.class));
break;
}
}

}


最后不要忘记在AndroidManifest.xml中配置Activity2和Activity3的信息


<activity 
android:name="com.example.activitysingleinstance.Activity2"
android:launchMode="singleInstance"
></activity>
<activity
android:name="com.example.activitysingleinstance.Activity3"
></activity>

现在我们把它部署到手机上。

按照这个打开顺序。

Activity1->Activity2->Activity3->Activity1->Activity2

我们看看在日志中的输出情况

android:Activity启动模式之singleInstance_singleInstance_02

分析:启动Activity1,创建一个Activity栈,ID=17.然后打开Activity2,因为Activity2为SingleInstance启动模式,这个实例要独自运行在一个栈中,我们会发现Activity2又新建了一个Activity栈,ID=18.然后再打开Activity3,进入ID=17的栈中,再打开Activity1继续进入ID=17的栈中。我们再次打开Activity2发现已经栈中已经有了Activity2的实例,不会创建新的实例,但会调用其onNewIntent方法。

具体栈的情况如下图

android:Activity启动模式之singleInstance_ide_03

需要按几次返回能回到主界面呢?聪明的你知道了把~那你也就理解了singleInstance启动模式

·4次~