The event that captures the back key on the Android platform, Super.onbackpressed () is the default action of the execution system, which is to exit the current activity, all we have to do is rewrite the onbackpressed () function,
Public void onbackpressed () { Toast.maketext (This, "You clicked the back key", Toast.length_long). Show ();
Realized in the Activity.class.
Public voidonbackpressed () {NewAlertdialog.builder ( This). Settitle ("Confirm exit?"). "). SetIcon (Android. R.drawable.ic_dialog_info). Setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //Click "Confirm" after the actionMainactivity. This. Finish (); }}). Setnegativebutton ("Back",NewDialoginterface.onclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //Click "Back" after the action, not set here No action}}). Show (); }
Related articles:
Android: Press again to exit the program