Android handles the back key press event

Source: Internet
Author: User

 Packagecom.example.keyevent;ImportAndroid.os.Bundle; Importandroid.view.KeyEvent; Importandroid.app.Activity; /*** Demo Description: * Handling back Key Press event * * NOTE: * The following two ways not to use together*/   Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);      Setcontentview (R.layout.activity_main); }      /*** Listen to the back key press event, Method 1: * super.onbackpressed () will automatically call the finish () method to execute OnDestroy (), close * Current activity. * To block the back keyboard, comment the line code to*/@Override Public voidonbackpressed () {Super. onbackpressed (); System.out.println ("Press back Key onbackpressed ()"); }                /*** Listen to the back key press event, Method 2: * Returns false here, so the event will continue to propagate. Continue execution of Super.onkeydown (KeyCode, event); */@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {          if(KeyCode = =keyevent.keycode_back)) {System.out.println ("Press back Key OnKeyDown ()"); return false; }Else {              return Super. OnKeyDown (KeyCode, event); }} @Overrideprotected voidOnDestroy () {Super. OnDestroy (); System.out.println ("Execute OnDestroy ()"); }     }  

Android handles the back key press event

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.