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"/>