I\'m using the google example to insert a datepicker inside my app using a dialogfragment
http://developer.android.com/guide/topics/ui/controls/pickers.html
But
I had a similar problem to this, but my date picker was being launched from a dialog fragment inside another fragment. This made it very difficult to play around with the callbacks, because they want to return to the Main Activity and I wanted the data to go back to the previous dialog fragment.
Initially, I passed the new date values to the Main Activity (using the OnDateSetListener) and was going to get them using the dialog that launched the date picker, but there are no lifecycle events triggered in that dialog when the datepicker closes.
The result I came to was in onDismiss in the date picker, instantiate a new dialog fragment, call a set results method on it and then launch it. Of course for this you need to make sure that the previous fragment is dismissed when it launches the date picker.
This is the dialogFragment that called the date picker
public class NewTrialDialogFragment extends DialogFragment {
public final String LOG_TAG = "NewTrialDialog Fragment";
Button startDateButton;
Integer newYear, newMonth, newDay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_new_trial, container, false);
getDialog().setTitle("Dialog New Trial");
startDateButton = (Button) rootView.findViewById(R.id.button_start_date);
startDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog(v);
}
});
//If you exit the datepicker without choosing anything, it returns zeros
if (newYear != null && newMonth != null && newDay != null && newYear != 0) {
startDateButton.setText(newYear + " " + newMonth + " " + newDay);
}else{
startDateButton.setText("Select Start Date");
}
return rootView;
}
public void showDatePickerDialog(View v) {
TrialDatePickerDialog newDialog = new TrialDatePickerDialog();
newDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
this.dismiss();
}
public void setDates(int year, int month, int day){
newYear = year;
newMonth = month;
newDay = day;
}}
And the date picker
public class TrialDatePickerDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private final String LOG_TAG = "TrialDatePickerDialog";
int newYear, newMonth, newDay;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of TrialDatePickerDialog and return it
return new DatePickerDialog(getActivity(), this , year, month, day);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
FragmentManager fm = getActivity().getSupportFragmentManager();
NewTrialDialogFragment newTrialDialogFragment = new NewTrialDialogFragment();
newTrialDialogFragment.setDates(newYear, newMonth, newDay);
newTrialDialogFragment.show(fm, "new_trial_dialog");
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
newYear = year;
newMonth = monthOfYear;
newDay = dayOfMonth;
}
}