Introduction to the use of Android back key onbackpressed ()

Source: Internet
Author: User

How to capture back key events on Android platform, back key is the return button on the phone, the general software does not capture relevant information may cause your program to be switched to the background, and the awkward situation of returning to the desktop, there are two ways on Android to get the event of the button.

1. Direct access to the button press event, this method is compatible with Android 1.0 to Android 2.1 is also the general method, directly rewrite the activity of the OnKeyDown method, the code is as follows:

@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back && event.getrepeatcount () = = 0) {//pressed if it is back and not duplicated
Toast.maketext (Ml78.this, "Magic go back Key test", 1). Show ();
return true;
}

Return Super.onkeydown (KeyCode, event);
}

For Android 2.0, there is a new method, for the activity can be separately get back key press event, directly rewrite the onbackpressed method, the code is as follows

@Override
public void onbackpressed () {
Here's the logic code, People Note: This method is only available for 2.0 or newer SDK
Return
}

Introduction to the use of Android back key onbackpressed ()

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.