40. Differences between the Android home key and the back key

Source: Internet
Author: User

Back key
Android programs do not need to exit intentionally. When you press the back key of the mobile phone, the system will call the Destroy () method of the top Activity in the program stack by default to Destroy the current Activity, when this Activity is started by another Activity, it will call the OnCreate () method again to create it. When all the activities in the stack Are popped up, the application will end. if there are services in the program, you can monitor and process them at the right location.


Home Key
Android program hiding: When you press the Home Key of your mobile phone, the system will call the stop () method of the top Activity in the program stack by default, and the entire application will be hidden, when you click the application icon on the desktop again, the system will call the OnResume () method of the upper-level Activity. Instead of opening the program again, the system will directly enter, the Activity at the top of the program stack is displayed.

To hide the program when you press the Home key:
1: Before Android 2.0, you need to listen to the button event to determine if the back key is pressed.
2: After Android 2.0, the system provides an onBackPressed () method, which is used to listen for back-key events. Therefore, you only need to override the onBackPressed () method.

@ Overridepublic void onBackPressed () {// implement the Home key effect // super. onBackPressed (); you must note this sentence, otherwise you will call the default back processing method Intent I = new Intent (Intent. ACTION_MAIN); I. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); I. addCategory (Intent. CATEGORY_HOME); startActivity (I );}

Exit application implementation: you can write a method by 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 must add the following permissions: <uses-permission android: name = "android. permission. RESTART_PACKAGES"/>
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.