When you redefine your onBackPressed, after the second click you need:
complete the application process:
ActivityManager am = (ActivityManager) getSystemService (Activity.ACTIVITY_SERVICE); am.killBackgroundProcesses (PackageName);
complete your first action. Here is an example: fooobar.com/questions/44987 / ...
UPD
The first case:
@Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { ActivityManager manager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> activityes = ((ActivityManager) manager).getRunningAppProcesses(); for (int i = 0; i < activityes.size(); i++){ if (activityes.get(i).processName.equals(getApplicationInfo().packageName)) { android.os.Process.killProcess(activityes.get(i).pid); } } return; } doubleBackToExitPressedOnce = true; new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; myOwnBackPress(); } }, 1000); } private void myOwnBackPress() { if(!isFinishing()) { super.onBackPressed(); } }
In fact, google does not recommend killing processes.
Second case:
//for all activity besides HomeActivity. @Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { Intent intent = new Intent(getApplicationContext(), HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent); return; } doubleBackToExitPressedOnce = true; new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; myOwnBackPress(); } }, 1000); } private void myOwnBackPress() { if(!isFinishing()) { super.onBackPressed(); } }
In your HomeActivity, do not override onBackPressed and add the following to onCreate:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
QArea source share