Android returns two buttons to exit the program, and android keys to exit the program.
Public boolean onKeyDown (int keyCode, KeyEvent event) {// two return exit program if (keyCode = KeyEvent. KEYCODE_BACK) {if (System. currentTimeMillis ()-mExitTime)> 2000) {Toast. makeText (this, R. string. toast_twice_press_back_to_exit, Toast. LENGTH_SHORT ). show (); mExitTime = System. currentTimeMillis () ;}else {finish () ;}return true ;}return super. onKeyDown (keyCode, event );}
The function is placed in your own Activity. Remember to define the mExitTime variable and initialization. It has been verified and the effect is good!
How to add a connection in android and press the exit key twice to exit the program
The principle is to calculate the time interval between two presses. You can set the value by yourself. If the interval is smaller than the specified value, it is considered to be two consecutive presses. At this time, exit
Private long mExitTime;
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
If (System. currentTimeMillis ()-mExitTime)> 800 ){
Toast. makeText (this, "exit the program again", Toast. LENGTH_SHORT). show ();
Mexico time = System. currentTimeMillis ();
} Else {
Finish ();
}
Return true;
}
Return super. onKeyDown (keyCode, event );
}
In the Android program, press the return key to exit the program. I want to press the return key to return to the previous interface. How should I implement this?
If you use setcontentview, that is, the activity is not actually switched, you have to handle the event that returns the key by yourself. When you click return, setcontentview is the one on the previous page.