Example analysis of the difference between home and back keys in Android _android

Source: Internet
Author: User

This example analyzes the difference between the home and back keys in Android. Share to everyone for your reference. Specifically as follows:

Back key

Android programs do not need to quit deliberately, when you press the phone's Back button, the system will call the top activity in the stack Destroy () method to destroy the current activity, When the activity is started by another activity, the OnCreate () method is called back to create, and the application ends when all the activity in the stack pops up. If there's a service in the program, Can be in the right place to listen to the processing can also be.

Home key

Hidden in the Android program, when you press the home button on your phone, the system defaults to the Stop () method of the top-level activity in the stack, and the entire application is hidden, and when you click on the app icon on your mobile desktop again, The system invokes the Onresume () method of the top-level activity, which does not reopen the program, but instead goes directly to the top-level activity in the program stack.

To implement the effect of hiding a program when you press the home key:

1. Android 2.0 needs to monitor key events before deciding whether to press the back key
2. After Android 2.0, the system provides a onbackpressed () method that is designed to monitor the back key event, so simply rewrite the onbackpressed () method to

@Override public
void onbackpressed () { 
  //implementation of the home key effect 
  //super.onbackpressed (); This sentence must be dropped, Or you go to the default back-handling mode. 
  Intent i= New Intent (intent.action_main); 
  I.setflags (intent.flag_activity_new_task); 
  I.addcategory (intent.category_home); 
  StartActivity (i); 
}

Exit the implementation of the application: You can write a method yourself, for example:

public void Exitprogrames () { 
  Intent startmain = new Intent (intent.action_main); 
  Startmain.addcategory (intent.category_home); 
  Startmain.setflags (intent.flag_activity_new_task); 
  StartActivity (startmain); 
  Android.os.Process.killProcess (Android.os.Process.myPid ()); 
}

Note: You need to add permissions: <uses-permission android:name= "Android.permission.RESTART_PACKAGES"/>

I hope this article will help you with your Android program.

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.