In Android app, we have custom AppCompatDialog class and need to open the soft input method (keyboard) on EditText after dialog was dismissed.
For showing the keyboard, this function is used:
fun View.showKeyboard() = context.inputMethodManager?.let {
// Find the really focused EditText in this root view.
val focusedView = if (this is ViewGroup) this.findFocus() else this
it.showSoftInput(focusedView ?: this, InputMethodManager.SHOW_IMPLICIT)
}
In dialog OnDismissListener, when calling this code below in Fragment, the code produces the expected outcome like in 70% of cases, but in order to work always in emulator and real devices, arbitrary 50ms delay time was needed to add for postDelayed.
view?.post {
binding.input.apply {
// binding.input is our custom Widget that contains EditText
requestFocus()
showKeyboard()
}
}
I would prefer to not have this magic number in the codebase and also I'm afraid, this might not work in some Android distributions.
Is there any better proven solution, that will work on most Android versions and vendor specific distributions?
Thanks for any help