code: @Override
      public boolean dispatchKeyEventPreIme(KeyEvent event) {
  
          if (mKeyboardMandatory) {
              if (getContext() != null) {
                  InputMethodManager imm = (InputMethodManager) getContext()
                          .getSystemService(Context.INPUT_METHOD_SERVICE);
  
                  if (imm.isActive() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                      setImeVisibility(true);
                      return true;
                  }
              }
          }
          return super.dispatchKeyEventPreIme(event);
      }
docs: Keyboard back button