第一个坑
首先场景是在两个Activity之间跳转,第二个 Activity使用setResult返回值,第二个Activity在软键盘没有收回的情况下finish,软键盘会收起,然后回到第一个Activity软键盘重新唤醒,需要点击其他控件转移焦点才会恢复
最开始没有意识到是焦点的问题,以为是第一个Activity上有什么东西抢了焦点,然后发现确实没有,就去第二个Activity上finish前手动关闭软键盘,发现可以解决问题
那么问题应该就是在页面退出时焦点没有消失上,正常来说页面销毁,控件销毁,焦点应该自动消失的,然后看了下堆栈,哦,原来是至少暂时处于onPause 而不是onDestroy和onStop,但是一想也不对啊, onPause就意味着焦点消失啊
而且很诡异的是,软键盘是在第二个Activity进入onPause时消失了,然后第一个Activity走入onResume后重新被打开了,所以跟焦点应该也没有关系,那剩下的可能就是在输入法或者OPPO魔改系统上面了
第二个坑
在收回软键盘时发现,下面这个逻辑实效
val imm = ContextCompat.getSystemService(this, InputMethodManager::class.java) if (imm != null && imm.isActive) { imm.hideSoftInputFromWindow(window.decorView.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY) }
应用是因为,软键盘显示时参数为:
/** * Flag for {@link #showSoftInput} to indicate that the user has forced * the input method open (such as by long-pressing menu) so it should * not be closed until they explicitly do so. */ public static final int SHOW_FORCED = 0x0002;
可以采用
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) { return; } imm.hideSoftInputFromWindow(view.getWindowToken(), 0);