Saving listener after screen rotation - DialogFragment with DatePicker

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");   
}   
+4
2

. : DateDialogFragment . CreateCreate . , showDatePickerDialog() , .

, :

DateDialogFragment frag;
DateDialogFragment.DateDialogFragmentListener mListener;
private EditText birthdate;
private Calendar now;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mListener = new DateDialogFragment.DateDialogFragmentListener() {
        @Override
        public void updateChangedDate(int year, int month, int day) {                               
            now.set(year, month, day);
            pet.setBirthdate(now.getTimeInMillis());
            birthdate = (EditText) findViewById(R.id.birth_day_et);     
            birthdate.setText(pet.getBirthdateInText());
        }
    };  

    if (savedInstanceState != null) {   
        pet.setBirthdate(savedInstanceState.getLong("birthday"));           
        now = (Calendar) savedInstanceState.getSerializable("n");
        if (now != null) {
            frag = (DateDialogFragment) savedInstanceState.getParcelable("frag");
            frag.setListener(mListener);                
        }
    } 

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
        outState.putLong("birthday", pet.getBirthdateInMillis());
        outState.putSerializable("n", now);
        outState.putParcelable("frag", frag);           
    }
}

public void showDatePickerDialog() {    
    now = Calendar.getInstance();   
    frag = DateDialogFragment.newInstance(this, mListener, now);
    frag.show(getSupportFragmentManager(), "DateDialogFragment");   
}   

DateDialogFragment. , , , .

public class DateDialogFragment extends SherlockDialogFragment implements Parcelable {

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel arg0, int arg1) {      
}

public static void setListener(DateDialogFragmentListener listener) {
    mListener = listener;
}

, , , . , - .:)

+3

. , Luksprog. .

if (savedInstanceState != null) {
    DatePickerFragment dpf = (DatePickerFragment) getSupportFragmentManager()
            .findFragmentByTag("theTag?");
    if (dpf != null) {
        dpf.setListener(listener);
    }
}

, ...

+6

All Articles