SoFunction
Updated on 2025-04-04

Detailed explanation of the application and implementation method of Android Back key click twice

Idea: The capture of user keys in Android is in the onKeyDown method. You only need to determine whether the user key is KEYCODE_BACK, that is, the back key, and the rest is to determine the time interval between the two clicks of BACK keys.

The first way to implement

 package ; 

  import ; 
  import ; 
  import ; 
  import ; 
  import ; 
  import ; 
  import ; 

  public class MainActivity extends Activity { 

    private static final String TAG = MainActivity_Exit.(); 

    // Define a variable to identify whether to exit    private static boolean isExit = false; 

    private static Handler mHandler = new Handler() { 

      @Override 
      public void handleMessage(Message msg) { 
        (msg); 
        isExit = false; 
      } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      (savedInstanceState); 
      setContentView(.activity_main); 

    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_BACK) { 
        exit(); 
        return true; 
      } 
      return (keyCode, event); 
    } 

    private void exit() { 
      if (!isExit) { 
        isExit = true; 
        (getApplicationContext(), "Press the back key again to exit the program", 
            Toast.LENGTH_SHORT).show(); 
        // Use handler to delay sending change status information        (0, 2000); 
      } else { 

        (TAG, "exit application"); 

        (); 
      } 
    } 
  }

In the exit method, the value of isExit will be judged first. If it is false, it will be set to true. At the same time, a prompt will be popped up, and a message will be sent after 2000 milliseconds (2 seconds), and the value will be restored to false in the Handler. If the BACK key is pressed again within 2 seconds of sending the message, the exit method is executed again. At this time, the value of isExit is true, the exit method will be executed.

The second way to implement it

 package ; 

  import ; 
  import ; 
  import ; 
  import ; 
  import ; 

  public class MainActivity extends Activity { 

    private static final String TAG = (); 

    private long clickTime = 0; //Record the time of the first click
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      (savedInstanceState); 
      setContentView(.activity_main); 

    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_BACK) { 
        exit(); 
        return true; 
      } 
      return (keyCode, event); 
    } 

    private void exit() { 
      if ((() - clickTime) > 2000) { 
        (getApplicationContext(), "Press the back key again to exit the program", 
            Toast.LENGTH_SHORT).show(); 
        clickTime = (); 
      } else { 
        (TAG, "exit application"); 
        (); 
  //     (0); 
      } 
    } 
  }

Determine whether the time difference between the user's two keys is within an expected value. If yes, exit directly. If not, prompt the user to press the back button again to exit.

Thank you for reading, I hope it can help you. Thank you for your support for this site!