title: How do I put previously selected date in an Android DatePickerDialog?
body: <p>I have a simple DatePickerFragment that loads today's date as the default date into a DatePickerDialog that is shown to the user.  The user then selects a date from the DatePicker and the code then sets that date on an EditText Line.  If the user later clicks again on the EditText line to edit their selected date, I want the Dialog to show the selected date, not today's date.  </p>
  
  <p>I use an emptiness test on the EditText line.  If empty, set today's date in the Dialog.  If not empty, then set the existing date in the Dialog from the EditText Line that the user previously selected.  So far I have tried to pass the previously selected date in a Bundle from onDateSet back to onCreateDialog.  I am getting a Fatal Exception:  "Fragment already active" so my logic must be set up in an incorrect loop with the fragment.  What am I missing here?</p>
  
  <pre><code>partial Fragment file:
  
  import android.content.DialogInterface;
  import android.support.v4.app.DialogFragment;
  import android.app.Dialog;
  import android.app.DatePickerDialog;
  import android.view.inputmethod.InputMethodManager;
  import android.os.Bundle;
  import android.widget.DatePicker;
  import android.widget.EditText;
  import java.util.Calendar;
  
  public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
  
  private EditText txtDate;
  private int currentyear;
  private int currentmonth;
  private int currentday;
  private int prvselectedyear;
  private int prvselectedmonth;
  private int prvselectedday;
  
  private String stringDueDateFrag;
  
  public DatePickerFragment() {
  }
  
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
  
      txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
  
      // If the FEditText line is empty (no "Due Date") entered then add current date to a new DatePickerDialog.
      if (txtDate.getText().length() == 0) {
      // DatePickerDialog is a simple dialog containing a DatePicker.
      // Set up a Calendar object that will capture the current date for the DatePickerDialog to use.
      // Use the current date as the default date in the DatePicker.
      final Calendar cal = Calendar.getInstance();
      currentyear = cal.get(Calendar.YEAR);
      currentmonth = cal.get(Calendar.MONTH);
      currentday = cal.get(Calendar.DAY_OF_MONTH);
  
      // Create a new instance of DatePickerDialog by passing 5 parameters/arguments to the constructor and return it.
      DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, currentyear, currentmonth, currentday);
      dialog.getDatePicker().setCalendarViewShown(true);
      dialog.getDatePicker().setSpinnersShown(false);
      dialog.setTitle("Select a Due Date");
  
      return dialog;
      }
  
      // If the FEditText line already has a due date then put the user's previously selected date in
      // the DatePickerDialog to edit.
      else {
         final Calendar prvcalendar = Calendar.getInstance();
         prvselectedyear = prvcalendar.get(Calendar.YEAR);
         prvselectedmonth = prvcalendar.get(Calendar.MONTH);
         prvselectedday = prvcalendar.get(Calendar.DAY_OF_MONTH);
  
         // Create a new instance of DatePickerDialog by passing 5 parameters/arguments to the constructor and return it.
         DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, prvselectedyear, prvselectedmonth, prvselectedday);
         dialog.getDatePicker().setCalendarViewShown(true);
         dialog.getDatePicker().setSpinnersShown(false);
         dialog.setTitle("Select a Due Date");
  
         return dialog;
      }
  }
  
  public void onDateSet (DatePicker view,int year, int month, int day) {
      txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
      stringDueDateFrag = (month + 1) + "/" + day + "/" + year + " ";
      txtDate.setText(stringDueDateFrag);
      txtDate.setSelection(txtDate.getText().length());
  
      // Set up a Calendar object to capture the user selected date in case the user wants
      // edit the date later.  Put the user selected date in a Bundle for onCreateDialog function to use.
      Calendar prvcalendar = Calendar.getInstance();
      Bundle argsbundle = new Bundle();
      argsbundle.putInt("year", prvcalendar.get(Calendar.YEAR));
      argsbundle.putInt("month", prvcalendar.get(Calendar.MONTH));
      argsbundle.putInt("day", prvcalendar.get(Calendar.DAY_OF_MONTH));
      setArguments(argsbundle);
  }
  </code></pre>
  
  <p>partial Activity file:</p>
  
  <pre><code>public class Activity extends AppCompatActivity {
  
  private ListenerEditText fListenerEditText;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.cardviewinput);
  
      fListenerEditText = (ListenerEditText) findViewById(R.id.FEditText);
  
      fListenerEditText.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              DialogFragment newFragment = new DatePickerFragment();
              newFragment.show(getSupportFragmentManager(), "datePicker");
          }
      });
  
      fListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
          @Override
          public void onFocusChange(View v, boolean hasFocus) {
              if (hasFocus &amp;&amp; (fListenerEditText.getText().length() == 0)) {
                  DialogFragment newFragment = new DatePickerFragment();
                  newFragment.show(getSupportFragmentManager(), "datePicker");
              }
          }
      });
  </code></pre>
  
  <p>Here is exception output from Android Studio:</p>
  
  <pre><code>09-19 03:18:43.052  17379-17379/com.example.jdw.fourthscreen W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa6310288)
  09-19 03:18:43.052  17379-17379/com.example.jdw.fourthscreen E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.IllegalStateException: Fragment already active
              at android.support.v4.app.Fragment.setArguments(Fragment.java:551)
              at com.example.jdw.fourthscreen.DatePickerFragment.onDateSet(DatePickerFragment.java:100)
              at android.app.DatePickerDialog.tryNotifyDateSet(DatePickerDialog.java:148)
              at android.app.DatePickerDialog.onClick(DatePickerDialog.java:116)
              at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4745)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>