I have a DatePickerFragment that launches a dialog for the user to select a date. The dialog uses today's date as the default date. Once the user selects a date the code updates an EditText line in the layout. If the user later goes back to the EditText line to edit the date, I want the dialog to the previously selected date, not the default date which is today's date. I read that onPrepareDialog() is deprecated so don't want to use that. I basically need to store the year, month and day that the user initially sets and then use those later rather than today's date when the dialog shows a second time.
partial Fragment file:
...
import android.support.v4.app.DialogFragment;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private EditText txtDate;
private Calendar cal;
private int currentyear;
private int currentmonth;
private int currentday;
private String stringDueDateFrag;
public DatePickerFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date that we get from a Calendar object, in the DatePicker Dialog.
final Calendar cal = Calendar.getInstance();
currentyear = cal.get(Calendar.YEAR);
currentmonth = cal.get(Calendar.MONTH);
currentday = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,currentyear,currentmonth,currentday);
return dialog;
}
txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
stringDueDateFrag = (month + 1) + "/" + day + "/" + year + " ";
txtDate.setText(stringDueDateFrag);
txtDate.setSelection(txtDate.getText().length());
}