When you click the EditText field in my (Scherlock) FragmentActivity, a DatePicker appears. After selecting the date and clicking "apply", the Edittext field is filled with the date. This works fine until I rotate the screen while the DatePicker is displayed. Its onCreateDialog method starts again and everything looks right. But when I click apply, no date is populated in the EditText field.
This is most likely because I was not able to save the listener. This problem is very similar to DialogFragment - saving the listener after turning the screen. However, in my code, I have a listener interface included in DialogFragment, and I canβt get to the listener from calling the FragmentActivity function (as the solution suggests in this question).
- I suppose not, but is there a way to save the listener before the action is destroyed? (I'm not interested in the Manifest modification.)
- Is there a way to change this DialogFragment or is this solution with the interface turned on not suitable in this case?
Please help me, it really should not be so difficult, but I worked with him longer than I admit ...
Here is the DialogFragment code:
public class DateDialogFragment extends SherlockDialogFragment {
public static String TAG = "DateDialogFragment";
static Context mContext;
static int mYear;
static int mMonth;
static int mDay;
static DateDialogFragmentListener mListener;
public static DateDialogFragment newInstance(Context context, DateDialogFragmentListener listener, Calendar now) {
DateDialogFragment dialog = new DateDialogFragment();
mContext = context;
mListener = listener;
mYear = now.get(Calendar.YEAR);
mMonth = now.get(Calendar.MONTH);
mDay = now.get(Calendar.DAY_OF_MONTH);
return dialog;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(mContext, mDateSetListener, mYear, mMonth, mDay);
}
private OnDateSetListener mDateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
mListener.updateChangedDate(year, monthOfYear, dayOfMonth);
}
};
public interface DateDialogFragmentListener {
public void updateChangedDate(int year, int month, int day);
}
}
: FragmentActivity: , :
public void showDatePickerDialog() {
now = Calendar.getInstance();
DateDialogFragment frag = DateDialogFragment.newInstance(
this, new DateDialogFragment.DateDialogFragmentListener() {
public void updateChangedDate(int year, int month, int day) {
birthdate.setText(String.valueOf(day) + "-" + String.valueOf(month+1) + "-" + String.valueOf(year));
now.set(year, month, day);
pet.setBirthdate(birthdate.getText().toString());
}
},
now);
frag.show(getSupportFragmentManager(), "DateDialogFragment");
}