Android, androiddeveloper
Double-click android to exit the current APP.
The basic idea for implementing this function is,
1. Listen to the backend key and compare the two backend intervals. If the value is less than two seconds, the system exits.
2. Exit the current APP.
I chose to set listening in BaseActivity of the base class. The Code is as follows:
@ Override public void onBackPressed () {// obtain whether to double-click in Preferences to exit boolean isDoubleClick = true; // BaseApplication. get ("ifDoubleClickedBack", true); if (isDoubleClick) {long curTime = SystemClock. uptimeMillis (); if (curTime-mBackPressedTime) <(2*1000) {finish (); // activity stack management AppManager. getAppManager (). appExit ();} else {mBackPressedTime = curTime; Toast. makeText (this, "click to exit again", Toast. LENGTH_SHORT ). show () ;}} else {finish ();}}
When exiting the current APP, refer to open source China and encapsulate the activity Stack
Public class AppManager {private static Stack <Activity> activityStack; private static AppManager instance; private AppManager () {}/ *** Single instance */public static AppManager getAppManager () {if (instance = null) {instance = new AppManager () ;}if (activityStack = null) {activityStack = new Stack <Activity> () ;}return instance ;} /*** get the specified Activity ** @ author kymjs */public static Activity get Activity (Class <?> Cls) {if (activityStack! = Null) for (Activity activity: activityStack) {if (activity. getClass (). equals (cls) {return activity;} return null;}/*** add Activity to stack */public void addActivity (Activity activity) {activityStack. add (activity);}/*** get the current Activity (the last pushed in the stack) */public Activity currentActivity () {Activity activity = activityStack. lastElement (); return activity;}/*** ends the current Activity (the last pushed in the stack) */publi C void finishActivity () {Activity activity = activityStack. lastElement (); finishActivity (activity);}/*** ends the specified Activity */public void finishActivity (Activity activity) {if (activity! = Null & activityStack. contains (activity) {activityStack. remove (activity); activity. finish () ;}/ *** ends the specified Activity */public void removeActivity (Activity activity) {if (activity! = Null & activityStack. contains (activity) {activityStack. remove (activity) ;}}/*** end Activity with the specified Class name */public void finishActivity (Class <?> Cls) {for (Activity activity: activityStack) {if (activity. getClass (). equals (cls) {finishActivity (activity); break ;}}/ *** ends all activities */public void finishAllActivity () {for (int I = 0, size = activityStack. size (); I <size; I ++) {if (null! = ActivityStack. get (I) {finishActivity (activityStack. get (I) ;}} activityStack. clear ();}/*** exit application */public void AppExit () {try {finishAllActivity (); // System. exit (0);} catch (Exception e ){}}}View Code
The idea is to manage each activity in the activity stack.
Traverse the stack when exiting, and finish one by one