Preface
When you click Back on the app main screen, there are usually several ways to exit
- Click to exit
- Pop-Up confirmation dialog
- Click to exit the program again
Personal favorite Way 3, compared to mode 2, do not need to move the finger to the center of the screen click Dialog, compared to the way 1, can prevent misoperation.
Implement
Rewrite the activity's Onbackpressed method, and when you click Back, record the current time backpressedtime and compare it with the backpressedtime of the last click. If the time difference is less than 1.5s, exit, or update backpressedtime.
long backpressedtime = system.currenttimemillis (); @Overridepublicvoid Onbackpressed () { long nowtime = system.currenttimemillis (); if (Nowtime-backpressedtime <) { Super. Finish (); Else { = nowtime; Toast.maketext (This, "Press again to exit the program", Toast.length_short). Show (); }}
References
- Android App series: Double-click the Back button to exit the program by enjoy wind chimes http://www.cnblogs.com/net168/p/4234869.html
Android Double click Back to exit the app