Android 解决InputMethodManager 内存泄露问题

在android 11的 InputMethodManager的源码中,查看 windowDismissed(),如下

/**
 * An empty method only to avoid crashes of apps that call this method via reflection and do not
 * handle {@link NoSuchMethodException} in a graceful manner.
 *
 * @deprecated This is an empty method.  No framework method must call this method.
 * @hide
 */
public void windowDismissed(IBinder appWindowToken) {
    
    
    // Intentionally empty.
    //
    // It seems that some applications call this method via reflection to null clear the
    // following fields that used to exist in InputMethodManager:
    //  * InputMethodManager#mCurRootView
    //  * InputMethodManager#mServedView
    //  * InputMethodManager#mNextServedView
    // so that these objects can be garbage-collected when an Activity gets dismissed.
    //
    // It is indeed true that older versions of InputMethodManager had issues that prevented
    // these fields from being null-cleared when it should have been, but the understanding of
    // the engineering team is that all known issues have already been fixed as of Android 10.
    //
    // For older devices, developers can work around the object leaks by using
    // androidx.activity.ComponentActivity.
    // See https://issuetracker.google.com/u/1/issues/37122102 for details.
    //
    // If you believe InputMethodManager is leaking objects in API 24 or any later version,
    // please file a bug at https://issuetracker.google.com/issues/new?component=192705.
}

/*
* 以下三个字段,在低于 android 10 的版本中是都存在的;之后,有变更。
* 低于 android 10,当Activity dismissed 时,可以通过反射,将它们置null,使它们不再持有该Activity中View的引用,防止内存泄露。
* InputMethodManager#mCurRootView
* InputMethodManager#mServedView
* InputMethodManager#mNextServedView
*
* android 10之后修复了所有已知问题。
* 对于较老的版本,建议使用 androidx.activity.ComponentActivity 及其子类。
* issues/37122102,说明 android N/7.0 (api 24),就开始了修复。
*
* android 11 source code:http://aospxref.com/android-11.0.0_r21/xref/frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
* android 10 source code:http://aospxref.com/android-10.0.0_r47/xref/frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
*/

解决方案:对于低于android10,且非 ComponentActivity的context,反射获取InputMethodManager对象中的 mCurRootViewmServedViewmNextServedView这三个属性,转换为View类型后,判断view的context等于要释放的Activity的context时,将这个属性置为null。

fun fixMemoryLeak(context: Context?) {
    
    
    try {
    
    
        context ?: return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) return // android 10 已修复
        if (context is ComponentActivity) return // androidx.activity.ComponentActivity 已修复
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager ?: return
        imm.javaClass.declaredFields.filter {
    
    
            it.name == "mCurRootView" || it.name == "mServedView" || it.name == "mNextServedView"
        }.forEach {
    
     filed ->
            val origin = filed.isAccessible
            if (!origin) {
    
    
                filed.isAccessible = true
            }
            (filed.get(imm) as? View)?.takeIf {
    
     it.context == context }?.also {
    
    
                filed.set(imm, null)
            }
            filed.isAccessible = origin
        }
    } catch (e: Exception) {
    
    
        e.printStackTrace()
    }
}

猜你喜欢

转载自blog.csdn.net/jjwwmlp456/article/details/124580048