Note, I apologize up front if this is already out there. In trying to figure this out, I found pieces of this out there, but never this whole issue & solution. So I thought I would write it up for the rest of the community.
There are lots of questions/answers out there on DatePickerDialogs (and TimePickerDialogs about how to capture the date/time set using onDateSet().
I found a nuance in this dialog functionality that produces what looks like a bug version 17 and below and I found a solution.
Here is the issue...
As has been stated out there, if you want to capture the date set upon leaving the DatePickerDialog you write and onDateSet() method and set the listener on the DatePickerDialog (typical as "this"). For example:
public static class MyDatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@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 DatePickerDialog and return it
DatePickerDialog dialog = new DatePickerDialog(getActivity(), **this**, year, month, day);
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
blah blah blah
do the date setting code
}
}
Notice that this that is passing the MyDatePickerFragment into the DatePickerDialog as the onDateSet() listener.
This works fine in android versions 17 and below. Try version 18 or higher and basically nothing happens. Your date set activities get lost. So another suggestion (I found) was to to add the following code to the onCreateDialog():
dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
getString(R.string.btn_cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which
{
if (which == DialogInterface.BUTTON_NEGATIVE) {
saved = false;
}
}
});
dialog.setButton(DialogInterface.BUTTON_POSITIVE,
getString(R.string.btn_done),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which
{
if (which == DialogInterface.BUTTON_POSITIVE) {
saved = true;
}
}
});
This works great for any version 18 or greater. That said, if you just add this to the onCreateDialog() then your version 17 and under quits working. Comment out this code version 17 and under works. Put it in, it quits and 18 and over works.
To fix this, I implemented it this way:
if(Build.VERSION.SDK_INT > 17) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
getString(R.string.btn_cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which
{
if (which == DialogInterface.BUTTON_NEGATIVE) {
saved = false;
}
}
});
dialog.setButton(DialogInterface.BUTTON_POSITIVE,
getString(R.string.btn_done),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which
{
if (which == DialogInterface.BUTTON_POSITIVE) {
saved = true;
}
}
});
}
Voila...!! Now both work. This same paradigm works for the TimePickerDialog as well.
I hope this helps.