The main purpose is to press the back button, so that the program can exit.
Realization of the idea:
1. When the back key event is caught, it is triggered.
Copy Code code as follows:
@Override
public boolean onKeyDown (int keycode, keyevent event) {
Pressed if it is back, without duplicating
if (keycode = = Keyevent.keycode_back && event.getrepeatcount () = = 0) {
Do something
....
}
return true;
}
2. Exit the program when the back key is pressed
There are also two ways to exit the program directly or exit to the main screen.
1 exit the program two ways:
Android.os.Process.killProcess (Android.os.Process.myPid ());
System.exit (0);
I tried these two methods, but strangely enough, the program was killed in the 1.5 simulator, but immediately Android restarted the program.
There is another way, more friendly, can release resources, I think it is feasible, is to achieve a bit of trouble, need to step out, rewrite the Onresume () method:
Copy Code code as follows:
@Override
protected void Onresume () {
Super.onresume ();
if (mexiting) {
Finish ();
}
}
2 Exit to main screen (recommended)
This is relatively simple, a new intent of the main screen can be.
Copy Code code as follows:
Intent home = new Intent (intent.action_main);
Home.setflags (Intent.flag_activity_clear_top);
Home.addcategory (Intent.category_home);
StartActivity (home);