The Android activity returns key control in two ways

Source: Internet
Author: User

1. Onbackpressed method for covering activityOfficial explanation:

Called when the activity had detected the user ' s press of the back key.

The default implementation simply finishes the current activity, but you can override this to do whatever for you want.

@Override
public void onbackpressed () {
It is entirely up to you to control the return key logic, and the system is not in control, but there is a precondition for not intercepting the return key in the activity's onkeydown or onkeyup. Intercept: It is in onkeydown or onkeyup that the return key is processed (returns true after processing). Or return false will cause onbackpressed not to execute)// Do not intercept: Returns the super corresponding method in OnKeyDown and OnKeyUp (if two methods are overwritten, each return super.onkeydown,super.onkeyup)
}2, overwrite the activity of the onkeydown or onkeyup, intercept the return button
@Override
public boolean onKeyUp (int keycode, keyevent event) {
if (keycode = = Keycode.keycode_back) {
return Iscosumenbackkey ();
}
return false;
}

Private Boolean Iscosumenbackkey () {
Here do the return key control, if you handle the return key logic to return True, if return false, the representative continues to pass down the back event, the system takes control
return true;
} Example: The following code does not go to onbackpressed (the following three methods are covered in one activity) @Override
public void onbackpressed () {

}

@Override
public boolean onKeyUp (int keycode, keyevent event) {
if (keycode = = Keycode.keycode_back)
return false;
return false;
}

@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keycode.keycode_back)
Return Super.onkeydown (KeyCode, event);
return false;
}

The Android activity returns key control in two ways

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.