1

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?

vandus
  • 3,248
  • 3
  • 30
  • 44
  • well the keyboard is up because the user is typing something. and he wants to hide the keyboard. so he clicks on the back button. but that action calls the contextual action bar to cancel itself as well... – vandus Apr 16 '14 at 22:35
  • No, I open a new fragment when user clicks on an item to edit the item. The CAB should stay visible throughout the whole time being in that fragment. Something like when you open a document in google drive. Only I think this is a regular behavior - any time you have your CAB shown and the keyboard is up and you want to hide the keyboard, you press the back button and the contextual action bar should be aware the back press was meant for the keyboard, not for the CAB. Again, it works like this in Google Drive app. – vandus Apr 17 '14 at 09:04
  • I don't think you are out of guidelines. When an user call a CAB, it is generally to search something in the current activity (or fragment). So to see the results he must to hide the keyboard. Also when the user does not find the result expected, he re-searches with the same CAB. Then, no, I don't think you're out of guidelines. – Blo Apr 17 '14 at 09:28

1 Answers1

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 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

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • Thank you, I´ll look into it. Please, see my last comment on my original question. I consider this to be a global problem, I think I am not doing anything against the guidelines.. – vandus Apr 17 '14 at 09:06