在應用中肯定遇到有這樣的問題,在應用中在於多的Activity中跳轉,這些Activity都存在Activity棧中了。所以按返回鍵的時候都是一個一個的將原來的Activity彈回。如果我們想捕獲到back事件之後直接退出整個程式,就要思考了。特別是2.2之後的安全機制考慮之後。 粘貼點代碼,以備之後使用。 Java代碼 package com.jftt; import Android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestLogout extends Activity { public static final String TAG = TestLogout.class.getSimpleName(); private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; private Button go; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.logout); this.onStart(); btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { android.os.Process.killProcess(android.os.Process.myPid()); // 擷取PID } }); btn2 = (Button) findViewById(R.id.btn2); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.exit(0); // 常規java、c#的標準退出法,傳回值為0代表正常退出 } }); btn3 = (Button) findViewById(R.id.btn3); btn3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "close " + getPackageName()); ActivityManager am = (ActivityManager) TestLogout.this .getSystemService(Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName()); // am.killBackgroundProcesses(getPackageName()); } }); btn4 = (Button) findViewById(R.id.btn4); btn4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); // intent.setClass((B或者C或者D).this, A.class); intent.setClass(TestLogout.this, TestLogout.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("flag", 1); startActivity(intent); } }); //此方法並未殺掉應用程式 而是把launcher調起 btn5 = (Button) findViewById(R.id.btn5); btn5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); } }); go = (Button) findViewById(R.id.go); go.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(TestLogout.this, TestLogout.class); // intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } }); } protected void onStart() { super.onStart(); Intent intent = getIntent(); int x = intent.getIntExtra("flag", -1); if (x == 0) { finish(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { AlertDialog.Builder alertbBuilder = new AlertDialog.Builder(this); alertbBuilder.setIcon(R.drawable.icon).setTitle("友情提示...").setMessage("你確定要離開嗎?"); alertbBuilder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 結束這個Activity int nPid = android.os.Process.myPid(); android.os.Process.killProcess(nPid); } }); alertbBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).create(); alertbBuilder.show(); } return true; } } Java代碼 package com.jftt; import java.util.Stack; import Android.app.Activity; public class ActiivtyStack { private static Stack<Activity> mActivityStack; private static ActiivtyStack instance; private ActiivtyStack() { } public static ActiivtyStack getScreenManager() { if (instance == null) { instance = new ActiivtyStack(); } return instance; } // 退出棧頂Activity public void popActivity(Activity activity) { if (activity != null) { activity.finish(); mActivityStack.remove(activity); // mActivityStack.pop(); activity = null; } } // 獲得當前棧頂Activity public Activity currentActivity() { Activity activity = mActivityStack.lastElement(); // Activity activity = mActivityStack.pop(); return activity; } // 將當前Activity推入棧中 public void pushActivity(Activity activity) { if (mActivityStack == null) { mActivityStack = new Stack<Activity>(); } mActivityStack.add(activity); // mActivityStack.push(activity); } // 退出棧中所有Activity public void popAllActivityExceptOne(Class<Activity> cls) { while (true) { Activity activity = currentActivity(); if (activity == null) { break; } if (activity.getClass().equals(cls)) { break; } popActivity(activity); } } } logout.xml Xml代碼 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="logout button1" /> <Button android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="logout button2" /> <Button android:id="@+id/btn3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="logout button3" /> <Button android:id="@+id/btn4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="go to first" /> <Button android:id="@+id/btn5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="go to launcher" /> <Button android:id="@+id/go" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="go another activity" /> <!-- <EditText android:id="@+id/et01" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <ImageView android:id="@+id/iv01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> --> </LinearLayout> manifest中的許可權: Xml代碼 <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> <uses-permission android:name="android.permission.RESTART_PACKAGE" />