Skip to content

Instantly share code, notes, and snippets.

@Kursulla
Created March 7, 2014 09:38
Show Gist options
  • Save Kursulla/9408501 to your computer and use it in GitHub Desktop.
Save Kursulla/9408501 to your computer and use it in GitHub Desktop.
Twice Back Button Exit [Android]
//Define as Actviity properties
private long backPressTime;
private static final int DOUBLE_BACK_TIME_LIMIT = 800;
//Define method that will perform the "magic"
private void doubleBackPressTrigger() {
if (backPressTime + DOUBLE_BACK_TIME_LIMIT > System.currentTimeMillis()) {
super.onBackPressed();
}else {
Toast.makeText(getBaseContext(), "Press \"Back\" twice to get out of the application!", Toast.LENGTH_SHORT).show();
}
backPressTime = System.currentTimeMillis();
}
//Put in your activity you want to be enabled with "double back exit"
@Override
public void onBackPressed() {
doubleBackPressTrigger();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment