Skip to content

Commit 2392d1a

Browse files
committed
Fixed some memory leaks in the sample app
1 parent a2814ff commit 2392d1a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

sample/src/main/java/com/wdullaer/datetimepickerexample/DatePickerFragment.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,22 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
128128
dpd.setScrollOrientation(DatePickerDialog.ScrollOrientation.VERTICAL);
129129
}
130130
}
131-
dpd.setOnCancelListener(dialog -> Log.d("DatePickerDialog", "Dialog was cancelled"));
131+
dpd.setOnCancelListener(dialog -> {
132+
Log.d("DatePickerDialog", "Dialog was cancelled");
133+
dpd = null;
134+
});
132135
dpd.show(requireFragmentManager(), "Datepickerdialog");
133136
});
134137

135138
return view;
136139
}
137140

141+
@Override
142+
public void onDestroy() {
143+
super.onDestroy();
144+
dpd = null;
145+
}
146+
138147
@Override
139148
public void onResume() {
140149
super.onResume();
@@ -146,5 +155,6 @@ public void onResume() {
146155
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
147156
String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
148157
dateTextView.setText(date);
158+
dpd = null;
149159
}
150160
}

sample/src/main/java/com/wdullaer/datetimepickerexample/TimePickerFragment.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,22 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
120120
};
121121
tpd.setDisabledTimes(disabledTimes);
122122
}
123-
tpd.setOnCancelListener(dialogInterface -> Log.d("TimePicker", "Dialog was cancelled"));
123+
tpd.setOnCancelListener(dialogInterface -> {
124+
Log.d("TimePicker", "Dialog was cancelled");
125+
tpd = null;
126+
});
124127
tpd.show(requireFragmentManager(), "Timepickerdialog");
125128
});
126129

127130
return view;
128131
}
129132

133+
@Override
134+
public void onDestroy() {
135+
super.onDestroy();
136+
tpd = null;
137+
}
138+
130139
@Override
131140
public void onResume() {
132141
super.onResume();
@@ -141,5 +150,6 @@ public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int seco
141150
String secondString = second < 10 ? "0"+second : ""+second;
142151
String time = "You picked the following time: "+hourString+"h"+minuteString+"m"+secondString+"s";
143152
timeTextView.setText(time);
153+
tpd = null;
144154
}
145155
}

0 commit comments

Comments
 (0)