温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

解决android中EditText导致的内存泄漏问题

发布时间:2020-07-03 15:00:18 来源:网络 阅读:2377 作者:IT学无止境 栏目:移动开发

开发中用到了LeankCanary,在一个简单的页面中(例如 :仅仅 包含Edittext),也会导致内训泄漏,为此,我在网上找了大量资料,最终解决。
例如一个布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="12dp"
                android:background="@null"
                android:hint="4-20位不包含特殊字符"
                android:textSize="15dp" />

<LinearLayout>

以上会导致内存泄漏,是由于EditText引用了Activity的context的原因,在Activity销毁的时候,
由于Edittext持有对Activity的context的引用,导致Activity无法正常回收。
解决办法:重写EditText,将对Activity中Context的引用,改为对ApplicationContext的引用。
代码如下:

@SuppressLint("AppCompatCustomView")
public class BaseEditText extends EditText {

private static java.lang.reflect.Field mParent;

static {
    try {
        mParent = View.class.getDeclaredField("mParent");
        mParent.setAccessible(true);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
}

public BaseEditText(Context context) {
    super(context.getApplicationContext());
}

public BaseEditText(Context context, AttributeSet attrs) {
    super(context.getApplicationContext(), attrs);
}

public BaseEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context.getApplicationContext(), attrs, defStyleAttr);
}

@SuppressLint("NewApi")
public BaseEditText(Context context, AttributeSet attrs, int defStyleAttr,
                    int defStyleRes) {
    super(context.getApplicationContext(), attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onDetachedFromWindow() {
    try {
        if (mParent != null)
            mParent.set(this, null);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    super.onDetachedFromWindow();
}

}

然后xml中的布局引用自定义的EditText:

             <包名.路径.BaseEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="12dp"
                android:background="@null"
                android:hint="4-20位不包含特殊字符"
                android:textSize="15dp" />

另外,由于LinearLayout获取了焦点,也可能会导致内存的泄漏,
需要在Activity中的onDestroy里清除掉焦点:
LinearLayout.clearFocus();

再次测试,问题解决!

感谢 https://www.jianshu.com/p/e1b41fb80cdc 文章的启发。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI