LeakUtils

作者: CentForever | 来源:发表于2021-01-31 22:00 被阅读0次

    InputMethodManager(Application.ActivityLifecycleCallbacks)

    import android.content.Context
    import android.view.View
    import android.view.inputmethod.InputMethodManager
    
    // https://issuetracker.google.com/issues/37043700#comment29
    class InputMethodManagerLeakFix {
        companion object {
            fun fixInputMethod(context: Context?) {
                if (context == null) {
                    return
                }
                var inputMethodManager: InputMethodManager? = null
                try {
                    inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                } catch (the: Throwable) {
                    the.printStackTrace()
                }
                if (inputMethodManager == null) {
                    return
                }
                val declaredFields = inputMethodManager.javaClass.declaredFields
                for (declaredField in declaredFields) {
                    try {
                        if (!declaredField.isAccessible) {
                            declaredField.isAccessible = true
                        }
                        val obj = declaredField.get(inputMethodManager)
                        if (obj == null || obj !is View) {
                            continue
                        }
                        val view: View = obj as View
                        if (view.context === context) {
                            declaredField.set(inputMethodManager, null)
                        } else {
                            return
                        }
                    } catch (the: Throwable) {
                        the.printStackTrace()
                    }
                }
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:LeakUtils

          本文链接:https://www.haomeiwen.com/subject/fbnizktx.html