HypnoZ
HypnoZ

Reputation: 55

ANDROID - setOnDismissListener on a DialogFragment

I've got a DialogFragment used to display a TimePickerDialog when pressing a button, and I would like to change the text of the button to the new time set.

My problem is I can't call setOnDismissListener on the DialogFragment from my Activity.

This is my DialogDragment

 public class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int hour = Application.getSettings().getInt("hour", 11);
        int minute = Application.getSettings().getInt("minute", 11);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        FirstTimeLaunchSettingsActivity.hours = hourOfDay;
        FirstTimeLaunchSettingsActivity.minutes = minute;

    }
}

And this is how I call this DialogFragment

    public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(this.getFragmentManager(), "timePicker");
}

I've already thought making a Handler in my activity which would refresh the Activity every seconde, but that's not how I would like to solve the problem.

I just want, when I close my DialogFragment, my button to be set to the time I've entered.

Thanks for your help.

Upvotes: 3

Views: 7731

Answers (4)

Boonya Kitpitak
Boonya Kitpitak

Reputation: 3737

Here is another way to setOnDismissListener() to the dialog fragment

FragmentManager fm = getFragmentManager();

YourDialogFragment dialog = new YourDialogFragment();
dialog.show(fm,"Dialog Name");

fm.executePendingTransactions();
dialog.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                       //action when dialog is dismissed goes here
                    }
                });

To make sure that FragmentTransaction work has been performed. We need to call

fm.executePendingTransactions();

Otherwise NullPointerException may occur when calling setOnDismissListener().

Upvotes: 6

Atiq
Atiq

Reputation: 14835

I can't call setOnDismissListener on the DialogFragment from my Activity.

You can, all you have to do is implement the interface in your activity

then Make your Activity implement OnDismissListener

public final class YourActivity extends AppCompatActivity implements DialogInterface.OnDismissListener {

    @Override
    public void onDismiss(final DialogInterface dialog) {
        //Fragment dialog had been dismissed
    }
}

Then just override the onDismiss listerenr in the fragment and call the Activity.

public final class DialogFragmentImage extends DialogFragment {
    @Override
    public void onDismiss(final DialogInterface dialog) {
        super.onDismiss(dialog);
        final Activity activity = getActivity();
        if (activity instanceof DialogInterface.OnDismissListener) {
            ((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
        }
    }

}

Upvotes: 0

devnull69
devnull69

Reputation: 16564

You could do it directly inside your already implemented onTimeSet method. You can get the activity where the TimePicker view is embedded and update the button text

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    FirstTimeLaunchSettingsActivity.hours = hourOfDay;
    FirstTimeLaunchSettingsActivity.minutes = minute;

    YourActivityClass activity = (YourActivityClass) getActivity();
    activity.updateButton(hourOfDay + ":" + (minute<10?"0"+minute:minute));
}

Then, inside your activity YourActivityClass, you can update the button as usual:

public void updateButton(String newText) {
    Button yourButton = (Button) findViewById(R.id.yourButtonID);
    yourButton.setText(newText);
}

Upvotes: 0

Daniel Zolnai
Daniel Zolnai

Reputation: 16910

You can override the onDismiss(DialogInterface dialog) method, which will be called when the DialogFragment is dismissed. You can also do it from your activity with an inline override:

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment() {
        @Override
        public void onDismiss(DialogInterface dialog){
            // Add your code here
        }
    };
    newFragment.show(this.getFragmentManager(), "timePicker");
}

Upvotes: 5

Related Questions