When developing Android applications, you often press the return key (that is, keycode = keyevent. keycode_back) to close the program. In most cases, the application is still running in the task. In fact, this is not the result we want.
We can do this. When a user clicks the custom exit button or return key (capture action is required), We forcibly exit the application in ondestroy () or directly kill the process, the specific operation code is as follows:
@ Override
Public Boolean onkeydown (INT keycode, keyevent event ){
// Press the return button on the keyboard
If (keycode = keyevent. keycode_back ){
New alertdialog. Builder (this)
. Seticon (R. drawable. Services)
. Settitle (R. String. Prompt)
. Setmessage (R. String. quit_desc)
. Setnegativebutton (R. String. Cancel, new dialoginterface. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int which ){
}
})
. Setpositivebutton (R. String. Confirm, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){
Finish ();
}
}). Show ();
Return true;
} Else {
Return super. onkeydown (keycode, event );
}
}
@ Override
Protected void ondestroy (){
Super. ondestroy ();
System. Exit (0 );
// Or the following method
// Android. OS. process. killprocess (Android. OS. process. mypid ());
}