It is necessary to give a hint before the user exits the application, because it is possible that the user does not really want to quit, but just accidentally pressed the return key, most of the practice is to give a dialog before the application exits, I think this is not very friendly, users have to move their fingers to press the dialog button. Personally feel "Press again return key exit program" is best practice, the implementation is also very simple, directly on the code:
Private long exittime = 0;
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back && event.getaction () = = Keyevent.action_down) {
if ((System.currenttimemillis ()-exittime) > 2000) {
Toast.maketext (Getapplicationcontext (), "Press again to exit the program", Toast.length_short). Show ();
Exittime = System.currenttimemillis ();
} else {
Finish ();
System.exit (0);
}
return true;
}
Return Super.onkeydown (KeyCode, event);
}
"Press the back key to exit the program" implementation in Android