SoFunction
Updated on 2025-03-11

Android implements the function of double-clicking to exit

Implement the function of android double-clicking back key to exit the current APP

The basic idea of ​​implementing this function is,

1. Listen to the back button, compare the two back intervals, and start and exit if it is less than two seconds.

2. Exit the current APP

I chose to set up listening in BaseActivity in the base class, the code is as follows:

 public void onBackPressed() {

    //Get in Preferences to double-click to exit    boolean isDoubleClick = true;
    //("ifDoubleClickedBack", true);

    if (isDoubleClick) {
      long curTime = ();
      if ((curTime - mBackPressedTime) < (2 * 1000)) {
        finish();
        //activity stack management        ().AppExit();
      } else {
        mBackPressedTime = curTime;
        (this, "Click to exit again", Toast.LENGTH_SHORT).show();
      }
    } else {
      finish();
    }

  }

In exiting the current APP, refer to open source China, encapsulate the activity stack

Example code:

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 getActivity(Class<?> cls) {
    if (activityStack != null)
      for (Activity activity : activityStack) {
        if (().equals(cls)) {
          return activity;
        }
      }
    return null;
  }

  /**
    * Add Activity to the Stack
    */
  public void addActivity(Activity activity) {
    (activity);
  }

  /**
    * Get the current activity (last pressed in the stack)
    */
  public Activity currentActivity() {
    Activity activity = ();
    return activity;
  }

  /**
    * End the current activity (last pressed in the stack)
    */
  public void finishActivity() {
    Activity activity = ();
    finishActivity(activity);
  }

  /**
    * End the specified activity
    */
  public void finishActivity(Activity activity) {
    if (activity != null && (activity)) {
      (activity);
      ();
    }
  }

  /**
    * End the specified activity
    */
  public void removeActivity(Activity activity) {
    if (activity != null && (activity)) {
      (activity);
    }
  }

  /**
    * End the activity of the specified class name
    */
  public void finishActivity(Class<?> cls) {
    for (Activity activity : activityStack) {
      if (().equals(cls)) {
        finishActivity(activity);
        break;
      }
    }
  }

  /**
    * End all activities
    */
  public void finishAllActivity() {
    for (int i = 0, size = (); i < size; i++) {
      if (null != (i)) {
        finishActivity((i));
      }
    }
    ();
  }

  /**
    * Exit the application
    */
  public void AppExit() {
    try {
      finishAllActivity();
      // (0);
    } catch (Exception e) {
    }
  }
}

The idea is to manage every time the activity is pushed into the activity stack.

Iterate through the stack when exiting, finish one by one

I hope this article will be helpful to everyone's Android programming design.