Search code examples
androidandroid-softkeyboardbackcabcontextual-action-bar

Contextual ActionBar hides when I click on hardware backbutton and the keyboard is out


I am using the Contextual ActionBar for editing and when I have the keyboard shown and I want to hide it by pressing the hardware back button, it hides the keyboard but it also cancels the contextual actionbar and I really can´t find a way how to keep it on.

Anyone?


Solution

  • You should try to override the Back Key hardware, and handle the expected behaviour with a boolean as follows:

    // boolean isVisible to retrieve the state of the SoftKeyboard
    private boolean isVisible = false;
    
    // isVisible becomes 'true' if the user clicks on EditText
    
    // then, if the user press the back key hardware, handle it:
    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // check isVisible
            if(isVisible) {
                // hide the keyboard
                InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                isVisible = false;
            } else {
                // remove the CAB
                mActionMode.finish();
            }
        }
        return false;
    }  
    

    Another solution might be to call dispatchKeyEvent method which is still called when the CAB is displayed:

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            // check CAB active and isVisible softkeyboard
            if(mActionModeIsActive && isVisible) {
                InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                isVisible = false;
                return true;
            // Maybe you might do not call the 'else' condition, anyway..
            } else {
                mActionMode.finish();
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }  
    

    This should do the trick, but I have not tested it. Hope this helps.
    Sources: How to Override android's back key when softkeyboard is open - Prevent to cancel Action Mode by press back button