莫名其妙的软键盘View内存泄漏

这个泄漏发生在我的Fragment页面销毁的时候,我既没有使用EditTextView,只有一个列表一个图片和一个视频播放View,刚看到的时候非常奇怪,我接受不了。
这个问题可能发生在不同情况下,Fragment销毁只是一种然后找了一下网上有这么个解决方式:

protected void fixSoftInputLeaks(final Activity activity) {
        //解决软键盘View内存泄漏Google的bug
        if (activity == null) return;
        InputMethodManager imm =
                (InputMethodManager) AppUtils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        String[] leakViews = new String[]{"mLastSrvView", "mCurRootView", "mServedView", "mNextServedView"};
        for (String leakView : leakViews) {
            try {
                Field leakViewField = InputMethodManager.class.getDeclaredField(leakView);
                if (leakViewField == null) continue;
                if (!leakViewField.isAccessible()) {
                    leakViewField.setAccessible(true);
                }
                Object obj = leakViewField.get(imm);
                if (!(obj instanceof View)) continue;
                View view = (View) obj;
                if (view.getRootView() == activity.getWindow().getDecorView().getRootView()) {
                    leakViewField.set(imm, null);
                }
            } catch (Throwable ignore) { /**/ }
        }
    }

只需要在你的发生泄露的Fragment或者在Activity的onDestroy中调用此方法即可。
看代码描述这应该是Google的bug,特此记录。

猜你喜欢

转载自blog.csdn.net/u011148116/article/details/108714746