配置:
在AndroidManifest.xml中找到<activity/>并在里面配上android:launchMode="启动模式"
如:
<activity android:name="com.njupt.luanchmode.Main2Activity" android:label="B界面"android:launchMode="singleInstance" />
常见类型(个人理解):
1)standard:不改变操作序列 .什么叫操作序列,例如你先进入A,在进入B,接着又进入A,最后又进入B。
那么你的操作序列就是A ----- >> B ------ >> A -------->>B。所以退出时的顺序就是反过来以后的结果
B------->>A------->>B------->>A
2)singleTop:合并相邻的重复项
3)singleTask:合并重复项
4)singleInstance:始终只保留最新的那一个
原理图:
1)standard:
2)singleTop
3)singleTask
4)singleInsatnce
实现如下:
1、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A界面"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开A界面"
android:onClick="open1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开B界面"
android:onClick="open2"
/>
</LinearLayout>
2、MainActivity
package com.njupt.luanchmode;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void open1(View v){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void open2(View v){
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3、main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B界面"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开A界面"
android:onClick="open1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开B界面"
android:onClick="open2"
/>
</LinearLayout>
4、Main2Activity
package com.njupt.luanchmode;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
public void open1(View v){
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
public void open2(View v){
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
}
}