内存泄露问题总结--static修饰的静态View
2017-01-04 20:36
369 查看
在自定义View的时候,为了方便View的创建,有些人会选择使用静态的方法创建View。在这里我们以自定义加载中Dialog为例,来讲述静态View造成内存泄露问题的解决方案。
我们先上Dialog的布局文件loading.xml:
再看CustomProgressDialog的Java文件:
其实其他的代码很正常,最关键的就是最后一个cancle方法:
在这里我们重写了Dialog的cancle()方法,我们在这个方法中将
然后在被调用的Activity或Fragment中需要的地方创建View,也一定要在Activity和Fragment的onDestory中调用View的onDestory()方法。
如果这个ViewA是被另一个ViewB所引用,也一定要在ViewB的onDestory()中电泳ViewA的onDestory()方法。
被static 修饰的View的创建和销毁一定要成对出现,而且我们也应该在意识里赋予自定义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生命周期的概念
相关文章推荐
- webView的内存泄露问题
- Android WebView 的内存泄露问题
- [Android] 关于WebView的内存泄露问题
- 最近在SPARK上定位的几个内存泄露问题总结
- 最近在SPARK上定位的几个内存泄露问题总结
- 自定义View时, 使用Canvas、Bitmap时易犯的内存泄露问题
- Android WebView 的内存泄露问题
- Android知识总结:图片放错mipmap文件夹导致内存泄露的问题
- 总结JavaScript在IE9之前版本中内存泄露问题
- 最近在SPARK上定位的几个内存泄露问题总结
- Js内存泄露问题总结
- org.springframework.web.servlet.view.InternalResourceViewResolver 内存泄露问题
- 搜索总结c++ 内存泄露问题
- 最近在SPARK上定位的几个内存泄露问题总结
- C++技术问题总结-第15篇 内存泄露有哪些方法定位,崩溃有哪些方法定位
- 内存泄露问题总结
- View重绘和内存泄露问题
- 项目问题总结:Block内存泄露 以及NSTimer使用问题
- 最近在SPARK上定位的几个内存泄露问题总结
- 最近在SPARK上定位的几个内存泄露问题总结