Search code examples
androidandroid-dialogfragment

Communication between two Dialog Fragment Android


I have one dialog fragment(AddAlarmDialogFragment) with a custom layout, which after click on the TextView is starting another TimePickerFragment.

TimePickerFragment is showing TimePickerDialog.

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    return new TimePickerDialog(getActivity(),TimePickerDialog.THEME_HOLO_LIGHT, this, hour, minute,
            true);

}

So far everything is working, but I want TimePickerFragment to pass the information about picked time to the parent DialogFragment

I made TimePickerFragment implementing TimePickerDialog.OnTimeSetListener, to override the callback method onTimeSet(TimePicker view, int hourOfDay, int minute).

I declared also my interface inside TimePickerFragment

    public interface OnTimePickedListener {
     void timePicked(int hour, int minutes);
}

The method timePicked(int hour, int minutes) is called inside onTimeSet(TimePicker view, int hourOfDay, int minute)

This is the whole code of the callBack

        @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        OnTimePickedListener onTimePickedListener=
OnTimePickedListener)getParentFragment();
        onTimePickedListener.timePicked(hourOfDay,minute);
    }

And here is a problem,that I want to pass the time from TimePickerDialog to the AddAlarmDialogFragment, so the DialogFragment which launched TimePickerFragment

Is there any way to get the instance of this interface from the parent dialogFragment. Or maybe there is a better way to provide a communication between this two components, because for me defining OnTimePickedListener which has almost exactly the same method as TimePickerDialog.OnTimeSetListener seem like not idea which is making code clean.


Solution

  • getParentFragment() is not getting the parent because you're using the FragmentManager of the Activity. In order for getParentFragment() to work, you need to use getChildFragmentManager() when performing your fragment transactions.

    However, for your purposes, you should just leverage the fragment tagging capability and find the fragment you're looking for:

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Fragment fragment = getActivity().getSupportFragmentManager()
           .findFragmentByTag(TimePickerFragment.TAG);
    
        if (fragment == null) {
            // the fragment we're looking for doesn't exist
            return;
        }
    
        try {
            OnTimePickedListener onTimePickedListener=(OnTimePickedListener)fragment;
            onTimePickedListener.timePicked(hourOfDay,minute);
        } catch (ClassCastException e) {
            // the fragment we're looking for doesn't implement the proper callback
            throw new ClassCastException(fragment.toString()
                    + " must implement OnTimePickedListener");
        }
    }