Use of the wirelessly onbackpressed () method

Source: Internet
Author: User

I. Interpretation of the onbackpressed () method

This method is placed in

void Android.app.Activity.onBackPressed ()

It explains this in the Android API:

public void onbackpressed ()

Added in API Level 5

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.

I translated it in a Youdao dictionary: The invoke activity detects the user's pressed key. The default implementation simply completes the current activity, but you can override this to do whatever you want.
My understanding is that this method will be called when the user presses the key.

This is the beginning of the Android 2.0 A new method, for the activity can be separately get back key press event, directly rewrite the onbackpressed method.

Ii. use of the onbackpressed () method

public void onbackpressed () {Intent intent=new Intent (); Intent.putextra ("Data_return", "Believe in Yourself"); Setresult (RESULT_OK, Intent); super.onbackpressed ();
return; }

Note: This method is only available for 2.0 or newer SDK

I do not know about this return, I have looked at a lot of information, said that the use of return but it is not clear what the effect of return. I hope that we can collect more information. I tried not to return the program can also be run.

Also, tell us the difference between the back key and the home key:

Back key
Android programs do not have to deliberately quit, when you press the back of the phone, the system will default to call the top activity in the program stack Destroy () method to destroy the current activity. When this activity is started again by other activity, the OnCreate () method is called again to create, and when all activity in the stack pops up, the application ends. If there is a service in the program, It is possible to listen to the processing in the right place.

Home key
Android program is hidden, when you press the phone's home button, the system will default to call the top activity in the program stack Stop () method, and then the entire application will be hidden, when you click on the Phone Desktop app icon again, The system invokes the Onresume () method of the topmost activity, which does not reopen the program, but directly enters, showing the topmost activity in the stack.

The back key defaults to the current activity to the Finish,home key only to the activity to OnStop.

Implement the effect of hiding a program when you press the home key:
1:android 2.0 need to listen to the key event, to determine whether the back button is pressed
After 2:android2.0, the system provides a onbackpressed () method, which is specifically used to listen to the back key event, so just rewrite the onbackpressed () method to

 Public void onbackpressed () {                 //  super.onbackpressed (); This sentence must be written off, or else to call the default back processing mode        Intent intent=New  Intent ();        Intent.putextra ("Data_return", "Believe in Yourself");        Setresult (RESULT_OK, intent);              }

About the onbackpressed () method I would like to introduce this, I hope to help you.

Use of the wirelessly onbackpressed () method

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.