Finish is the activity class. It only applies to the activity. When finish () is called, the activity is pushed to the background, and the memory is not released immediately. The activity resources are not cleared; when system. when exit (0), the entire process is killed, and the resources occupied by the activity are also released.
When developing Android applications, you often press the return key (that is, keycode =)KeyevenT. keycode_back).ProgramIn fact, 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 you click the custom exit button or return key (capture action is required ),Ondestroy ()To forcibly exit the application or directly kill the process.CodeAs 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 ());
}