If you create Time Picker Dialog with onCreateDialog and call it it will automatically store the previous value.
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
TimePickerDialog timeDlg = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
if (hourOfDay > 12) {
hourOfDay = hourOfDay - 12;
time = " PM";
} else if (hourOfDay < 12 && hourOfDay != 0) {
time = " AM";
} else if (hourOfDay == 12) {
time = " PM";
} else if (hourOfDay == 0) {
hourOfDay = 12;
time = " AM";
}
Toast.makeText(
getApplicationContext(),
new StringBuilder().append(pad(hourOfDay))
.append(":").append(pad(minute))
.append(time), Toast.LENGTH_SHORT)
.show();
}
}, 12, 00, false);
timeDlg.setMessage("Set Time:");
timeDlg.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Dismiss",
Toast.LENGTH_SHORT).show();
}
});
return timeDlg;
}
return null;
}
Use showDialog(id); to Show the Dialog.