Skip to content Skip to sidebar Skip to footer

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 can

Solution 1:

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 SoftKeyboardprivatebooleanisVisible=false;

// isVisible becomes 'true' if the user clicks on EditText// then, if the user press the back key hardware, handle it:@OverridepublicbooleanonKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // check isVisibleif(isVisible) {
            // hide the keyboardInputMethodManagermImm= (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            isVisible = false;
        } else {
            // remove the CAB
            mActionMode.finish();
        }
    }
    returnfalse;
}  

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 softkeyboardif(mActionModeIsActive && isVisible) {
            InputMethodManager mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            mImm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            isVisible = false;
            returntrue;
        // Maybe you might do not call the 'else' condition, anyway..
        } else {
            mActionMode.finish();
            returntrue;
        }
    }
    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

Post a Comment for "Contextual Actionbar Hides When I Click On Hardware Backbutton And The Keyboard Is Out"