In Android, what is the difference between finish () and system. Exit (0?

Source: Internet
Author: User

From: http://blog.csdn.net/xuyide54321/article/details/7049297


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,

At this time, the resources occupied by the activity will also be released.

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 you click the custom exit button or return key (capture action is required), We forcibly exit the application in ondestroy () or directly kill the process.

@ Overridepublic 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 () {@ overridepublic void onclick (dialoginterface dialog, int which ){}}). setpositivebutton (R. string. confirm, new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {finish ();}}). show (); Return true;} else {return Super. onkeydown (keycode, event) ;}@ overrideprotected void ondestroy () {super. ondestroy (); system. exit (0); // or the following method // android. OS. process. killprocess (Android. OS. process. mypid ());}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.