android退出應用程式會調用android.os.Process.killProcess(android.os.Process.myPid())或是System.exit(0),這隻是針對第一個Activity(也就是入口的Activity)時生效。如果有A,B,C三個Activity,而想在B或C中Activity退出,調用上面的方法,往往會銷毀當前的Activity返回上一個Activity。當然也可以逐個返回上一個Activity,直到跳轉到入口的Activity,最後退出應用程式。但這樣比較麻煩,而且逐個返回的體驗並不友好。
網上比較流行的方法是定義棧,寫一個ExitApplication類,利用單例模式管理Activity,在每個在Activity的onCreate()方法中調用ExitApplication.getInstance().addActivity(this)方法,在退出時調用ExitApplication.getInstance().exit()方法,就可以完全退出應用程式了。
ExitApplication類
代碼如下:
複製代碼 代碼如下:import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
public class ExitApplication extends Application {
private List activityList = new LinkedList();
private static ExitApplication instance;
private ExitApplication()
{
}
//單例模式中擷取唯一的ExitApplication執行個體
public static ExitApplication getInstance()
{
if(null == instance)
{
instance = new ExitApplication();
}
return instance;
}
//添加Activity到容器中
public void addActivity(Activity activity)
{
activityList.add(activity);
}
//遍曆所有Activity並finish
public void exit()
{
for(Activity activity:activityList)
{
activity.finish();
}
System.exit(0);
}
}
下面的三個類IndexActivity, BActivity,CActivity是簡單的例子,分別是IndexActivity–>BActivity–>CActivity的跳轉順序。在每個Activity類中onCreate()方法中調用ExitApplication.getInstance().addActivity(Activity activity)方法。在任何一個Activity介面退出應用程式時,只要調用ExitApplication.getInstance().exit()方法,就可以在任何一個Activity中完全退出應用程式。
IndexActivity 類原始碼:
複製代碼 代碼如下: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;
public class IndexActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next=(Button)findViewById(R.id.next_to_b);
next.setOnClickListener(nextClick);
Button exit=(Button)findViewById(R.id.exit_main);
exit.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this);
}
OnClickListener nextClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(IndexActivity.this,BActivity.class);
startActivity(intent);
}
};
OnClickListener exitClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
}
};
}
BActivity 類原始碼:
複製代碼 代碼如下: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;
public class BActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
Button next_to_c=(Button)findViewById(R.id.next_to_c);
next_to_c.setOnClickListener(next_to_cClick);
Button exit_b=(Button)findViewById(R.id.exit_b);
exit_b.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this);
}
OnClickListener next_to_cClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(BActivity.this,CActivity.class);
startActivity(intent);
}
};
OnClickListener exitClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
}
};
}
CActivity 類原始碼:
複製代碼 代碼如下:import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
Button exit_c=(Button)findViewById(R.id.exit_c);
exit_c.setOnClickListener(exitClick);
ExitApplication.getInstance().addActivity(this);
}
OnClickListener exitClick=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ExitApplication.getInstance().exit();
//如果只是調用以下其中的一個方法,並不會完全退出應用
//android.os.Process.killProcess(android.os.Process.myPid());
//System.exit(0);
}
};
}
源碼下載:ExitActivity.zip