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 LongExittime = 0; @Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if(KeyCode = =Keyevent.keycode_back&& event.getaction () = =Keyevent.action_down) { //exit the application if the interval is longer than 2 seconds if((System.currenttimemillis ()-Exittime) > 2000) {Toast.maketext (Getapplicationcontext (),"Press one more time to exit the program", Toast.length_short). Show (); Exittime=System.currenttimemillis (); } Else { //Close the applicationfinish (); System.exit (0); //back to desktop operations//Intent home = new Intent (intent.action_main);//home.addcategory (intent.category_home);//startactivity (home); } return true; } return Super. OnKeyDown (KeyCode, event);}
Yes, the comment section later mentions the return to the main interface, there are many applications do not want the user to exit directly, and the user does not want to quit the application, and the user just want to quickly back to the main interface, so that the app continues to run in the background. So, we can also let the user click Back to return to the main interface.
"Press the back key to exit the program" implementation in Android