0

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());
}
AJW
  • 1,578
  • 3
  • 36
  • 77

1 Answers1

0

On your onCreate() method you initialize the current date every time, you should pass the date from EditText to the DialogFragment everytime you calls it.

Similar question for references

Set date of datepickerdialog from EditText

Community
  • 1
  • 1
cw fei
  • 1,534
  • 1
  • 15
  • 33
  • So set up a Calendar object and a Bundle with the EditText date and then call the Bundle the next time the EditText is seleted? – AJW Sep 17 '15 at 03:29
  • Send the EditText date as bundle, receive the bundle at dialog fragement, convert the EditText date to date format and set it as current date, display dialog – cw fei Sep 17 '15 at 03:32
  • Ok and all of that is set up outside of OnCreate correct? – AJW Sep 17 '15 at 03:35