Android app Back key listening and processing instances

Source: Internet
Author: User

MainActivity is as follows: Copy codeThe Code is as follows: package cn. testnbackpressed;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. app. Activity;
/**
* Demo description:
* Process the Back key press event
*
* Note:
* Do not use the following two methods together
*/
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
/**
* Listen to the Back key press event, Method 1:
* Note:
* Super. onBackPressed () automatically calls the finish () method and closes
* Current Activity.
* To shield the Back keyboard, just comment out the line of code.
*/
@ Override
Public void onBackPressed (){
Super. onBackPressed ();
System. out. println ("the back key onBackPressed ()" is pressed ()");
}
/**
* Listen to the Back key press event, Method 2:
* Note:
* Indicates whether the event can be fully processed.
* False is returned here, so the event will continue to be propagated.
* In a specific project, the return value depends on the actual situation.
*/
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK )){
System. out. println ("the back key onKeyDown ()" is pressed ()");
Return false;
} Else {
Return super. onKeyDown (keyCode, event );
}

}

@ Override
Protected void onDestroy (){
Super. onDestroy ();
System. out. println ("execute onDestroy ()");
}
}

Main. xml is as follows: Copy codeThe Code is 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 methods for processing the Back key"
Android: layout_centerInParent = "true"
Android: textSize = "20sp"
/>
</RelativeLayout>

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.