A case of _android and processing of back key in Android application

Source: Internet
Author: User
mainactivity is as follows:
Copy Code code as follows:

Package cn.testnbackpressed;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import android.app.Activity;
/**
* Demo Description:
* Handle the Back key press event
*
* Precautions:
* The following two ways not to use together
*/
public class Mainactivity extends activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
/**
* Monitor the Back key press event, Method 1:
Attention
* super.onbackpressed () automatically calls the finish () method to close
* Current activity.
* To block the back keyboard, note that the line code can
*/
@Override
public void onbackpressed () {
Super.onbackpressed ();
System.out.println ("Press the Back Key onbackpressed ()");
}
/**
* Monitor the Back key press event, Method 2:
Attention
* The return value indicates whether the event can be fully handled
* Returns false here, so the event continues to propagate.
* The return value here depends on the circumstances of the specific project.
*/
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if ((keycode = = Keyevent.keycode_back)) {
System.out.println ("Press the Back Key OnKeyDown ()");
return false;
}else {
Return Super.onkeydown (KeyCode, event);
}

}

@Override
protected void OnDestroy () {
Super.ondestroy ();
SYSTEM.OUT.PRINTLN ("Executive OnDestroy ()");
}
}

Main.xml is as follows:
Copy Code code as follows:

<relativelayout
xmlns:android= "http://schemas.android.com/apk/" Res/android "
xmlns:tools=" Http://schemas.android.com/tools
android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
>
<textview
android:layout_width= "Wrap_content"
Android : layout_height= "wrap_content"
android:text= "two ways to handle the back key"
Android:layout_centerinparent= "true"
Android:textsize= "20sp"
/>
</RelativeLayout>

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.