Created
April 2, 2018 14:53
-
-
Save alitamoor65/903aefb928f3cfefc95c738cbeb48e6a to your computer and use it in GitHub Desktop.
Press Again to Exit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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