The realization method of the Android return key function _android

Source: Internet
Author: User

This example describes the implementation of the Android return key function. Share to everyone for your reference. Specifically as follows:

In the development of Android applications, often by pressing the return key (that is, keycode = = Keyevent.keycode_back) can close the program, in fact, most of the case did not turn off the application

We can do this, when the user clicks the Custom exit button or returns the key (needs to capture the action), we forcibly exits the application in the OnDestroy (), or kills the process directly, the concrete operation code is as follows:

public boolean onKeyDown (int keycode, keyevent event) {
  
//press the return button on the keyboard  
if (keycode = = keyevent.keycode_back) {  

  New Alertdialog.builder (This)  
  . Setmessage (OK to exit the system?) ")  
  . Setnegativebutton (" Cancel ",  
   new Dialoginterface.onclicklistener () {public
     void OnClick ( Dialoginterface dialog,
     int which) {  
     }  
   }  
  . Setpositivebutton (OK),  
   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 ();  
    Or the following way  
    //system.exit (0);  
    It is recommended to use this  
    android.os.Process.killProcess (Android.os.Process.myPid ());
}

I hope this article will help you with your Android program.

Related Article

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.