The native Android UI has many pitfalls which require workaround / hacks.
For example, when I display two dialogs (d1, d2), in d2 show an EditText and bring up the input method. If I click outside, I'll find that the second dialog d2 closes and the we return to the first dialog d1, but the keyboard doesn't disappear. Instead, it hides behind the first dialog. (Very strange!)
For this bug, none of the above answers worked ( under onDismiss ). I've tried : d1...hideSoftInputFromWindow; d2...hideSoftInputFromWindow respectively, and also attempted using postDelayed.., but nothing worked. Let alone using toggleSoftInput, which was even more unstable.
However, calling hideSoftInputFromWindow before onDismiss does work. Therefore, the problem with the dialog and the keyboard can be permanently solved with this DIRTY approach:
d.setOnDismissListener(dialogInterface -> { View f = d.getCurrentFocus(); if (f instanceof EditText) { d.show(); // dirty! f.clearFocus(); InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(f.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); f.post(d::hide);// dirty! } });
I've also tested on different devices with various input methods and am certain that this is a bug. (Not to mention I've seen this BUG a long time ago. )
In conclusion, Android UI is a somewhat outdated technology. Minor old bugs are no longer fixed, but the system is mature enough that these issues can be addressed using workarounds.
