I am trying to override the functionality of pressing the back key. When the user clicks once, I want him to return to the previous screen. However, when the back key is pressed (for example, two seconds or more), I want to exit the application.
I am currently redefining these two methods in my work:
@Override public boolean onKeyDown( int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_BACK) { //manage short keypress return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyLongPress( int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_BACK) { //manage long keypress (different code than short one) return true; } return super.onKeyLongPress(keyCode, event); }
But the onKeyLongPress never called because the event is always accepted by the onKeyDown method.
Is there a way to use both methods? Or do you need to do everything in onKeyDown and use the number of repetitions / milliseconds to detect it?
Mister smith
source share