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()方法,就可以完全退出應用程式了。
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並finishpublic 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中完全退出應用程式。
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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent(IndexActivity.this,BActivity.class);startActivity(intent);}}; OnClickListener exitClick=new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubExitApplication.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{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubExitApplication.getInstance().exit();//如果只是調用以下其中的一個方法,並不會完全退出應用//android.os.Process.killProcess(android.os.Process.myPid());//System.exit(0);}};}