Skip to content

Instantly share code, notes, and snippets.

@alitamoor65
Created April 2, 2018 14:53
Show Gist options
  • Save alitamoor65/903aefb928f3cfefc95c738cbeb48e6a to your computer and use it in GitHub Desktop.
Save alitamoor65/903aefb928f3cfefc95c738cbeb48e6a to your computer and use it in GitHub Desktop.
Press Again to Exit
private long lastPressedTime;
private static final int PERIOD = 2000;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
if (event.getDownTime() - lastPressedTime < PERIOD) {
finish();
} else {
Toast.makeText(getApplicationContext(), "Press again to exit.",
Toast.LENGTH_SHORT).show();
lastPressedTime = event.getEventTime();
}
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment