内存泄露问题总结--static修饰的静态View

在自定义View的时候,为了方便View的创建,有些人会选择使用静态的方法创建View。在这里我们以自定义加载中Dialog为例,来讲述静态View造成内存泄露问题的解决方案。
我们先上Dialog的布局文件loading.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_dialog"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/prog_title"
        style="@style/loading_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5.0dip"
        android:background="@drawable/login_title_bg"
        android:text="@string/loading"
        android:visibility="gone" >
    </TextView>

    <ProgressBar
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:indeterminateDrawable="@drawable/progressbar_bg" >
    </ProgressBar>

    <TextView
        android:id="@+id/prog_msg"
        style="@style/loading_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5.0dip"
        android:text="@string/loading" >
    </TextView>

</LinearLayout>

再看CustomProgressDialog的Java文件:

package com.hwapu.education.teacher.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.hp.classes.education.teacher.activity.R;
import com.orhanobut.logger.Logger;

/**
 * 自定义页面加载对话框
 * 
 * @author zhouyou
 * 
 */
public class CustomProgressDialog extends Dialog {
    // private Context context = null;
    private static CustomProgressDialog customProgressDialog = null;

    public CustomProgressDialog(Context context) {
        super(context);
        // this.context = context;
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public static CustomProgressDialog createDialog(Context context) {
        customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.loading);
        View view = customProgressDialog.findViewById(R.id.custom_dialog);
        view.setPadding(10, 10, 10, 10);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        Logger.d("CustomProgressDialog createDialog");
        return customProgressDialog;
    }

    public static CustomProgressDialog createDialog(Context context, int layoutid) {
        customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(layoutid);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        Logger.d("CustomProgressDialog createDialog2");
        return customProgressDialog;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (customProgressDialog == null) {
            return;
        }
    }

    /**
     * 
     * [Summary] setTitile 标题
     * 
     * @param strTitle
     * @return
     * 
     */
    public CustomProgressDialog setTitile(String strTitle) {
        TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.prog_title);
        if (tvMsg != null) {
            tvMsg.setVisibility(View.VISIBLE);
            tvMsg.setText(strTitle);
        }
        return customProgressDialog;
    }

    public CustomProgressDialog setBackgroudResource(int resourceid) {
        View view = customProgressDialog.findViewById(R.id.custom_dialog);
        if (view != null) {
            view.setBackgroundResource(resourceid);
        }
        return customProgressDialog;
    }

    public CustomProgressDialog setDialogSize(int w, int h) {
        customProgressDialog.getWindow().getAttributes().height = h;
        customProgressDialog.getWindow().getAttributes().width = w;
        View view = customProgressDialog.findViewById(R.id.custom_dialog);
        return customProgressDialog;
    }

    /**
     * 
     * [Summary] setMessage 提示内容
     * 
     * @param strMessage
     * @return
     * 
     */
    public CustomProgressDialog setMessage(String strMessage) {
        TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.prog_msg);
        if (tvMsg != null) {
            tvMsg.setText(strMessage);
            tvMsg.setTextColor(Color.WHITE);
        }
        return customProgressDialog;
    }

    @Override
    public void cancel() {
        customProgressDialog = null;
        super.cancel();
    }
}

其实其他的代码很正常,最关键的就是最后一个cancle方法:

public void cancel() {
        customProgressDialog = null;
        super.cancel();
    }

在这里我们重写了Dialog的cancle()方法,我们在这个方法中将customProgressDialog置空。当然我们如果是自定义的其他View,如果使用了static修饰,就必须为该View提供一个类似生命周期的方法,如onDestory(),在该方法中释放内存。

然后在被调用的Activity或Fragment中需要的地方创建View,也一定要在Activity和Fragment的onDestory中调用View的onDestory()方法。

如果这个ViewA是被另一个ViewB所引用,也一定要在ViewB的onDestory()中电泳ViewA的onDestory()方法。

被static 修饰的View的创建和销毁一定要成对出现,而且我们也应该在意识里赋予自定义View生命周期的概念

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值