在自定义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生命周期的概念