我在每个项目中都有一个带textview的列表视图,单击该列表时会启动一个带有数字选择器元素的对话框,以更改该项目中的值。当使用LeakCanary库检查内存泄漏时,我遇到了与对话框片段相关的内存泄漏,当我反复启动和关闭对话框时会显示该对话框。我也查看了Stack Overflow上的类似文章,但没有一个可以解决问题。对话框片段的实现如下:
public class NumberDialog extends DialogFragment {
NumberPicker numberPicker;
NumberPicker numberPicker2;
NumberPicker numberPicker3;
public static NumberDialog newInstance() {
NumberDialog frag = new NumberDialog ();
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View child = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
numberPicker = child.findViewById(R.id.numberPicker1);
numberPicker2 = child.findViewById(R.id.numberPicker2);
numberPicker3 = child.findViewById(R.id.numberPicker3);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(59);
numberPicker3.setMinValue(0);
numberPicker3.setMaxValue(59);
numberPicker2.setMinValue(0);
numberPicker2.setMaxValue(59);
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity(), R.style.Theme_Material_Dialog_Alert);
builder.setTitle("Choose Value");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
builder.setView(child);
return builder.create();
}
}
以下是泄漏源的屏幕截图: Screenshot 1