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.