1

I have an AppCompatActivity where I programmatically toggle the softinput. There is only one scenario where my keyboard open/close boolean is not set properly because I cannot intercept the back-button event. This event closes the keyboard when executed.

The following is printed when pressing the on-screen back button when a keyboard is opened.

I/ViewRootImpl@af03171[MainActivity]: The input has been finished in ImeInputStage.
  1. I've tried all the toggleSoftInput variants/flags I could find to pray it would somehow circumvent the issue
  2. I tried to catch the press with "dispatchKeyEvent", "backPressed", "onKeyUp", "onKeyDown", "onKeyPreIme" with no luck.

And as the message says the event is finishing somewhere else. I just can't find anything on the web to realize the functionality I need for my application.

In addition, this is my toggle function.

public void toggleSoftInput() {
        InputMethodManager imm = (InputMethodManager) getAndroidContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
        keyboardOpened = !keyboardOpened;
}

Edit: I've searched over any resource I could find and still haven't been able to resolve this edge case in my application. Any pointers are greatly appreciated.

linoidkl
  • 11
  • 2
  • You might want to switch gears and consider why you need `keyboardOpened` in the first place. After all, not every Android device uses a soft keyboard, and there is no guarantee that your `toggleSoftInput()` will have any effect. – CommonsWare Aug 13 '22 at 22:01
  • I have a custom application where this is required, no compliance issues will be prompted with my usage. I've utilized 'keyboardOpened' as adviced by many on stackoverflow and implemented it throughout my application. Everything works and is set correctly - except for this edge case that I cannot resolve. – linoidkl Aug 14 '22 at 08:12

0 Answers0