不可解なソフトキーボードビューのメモリリーク

このリークは、フラグメントページが破壊されたときに発生しました。EditTextViewを使用せず、リスト、画像、およびビデオ再生ビューしかありませんでした。最初に見たとき、それは非常に奇妙であり、受け入れることができませんでした。
この問題はさまざまな状況で発生する可能性があります。フラグメントの破棄は1つの方法にすぎません。そのような解決策をオンラインで探しました:

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またはアクティビティのonDestroyだけです。
コードの説明を見てください。これはGoogleのバグであり、ここに記録されています。

おすすめ

転載: blog.csdn.net/u011148116/article/details/108714746