John
John

Reputation: 336

Android setOnDismissListener not getting called inside onCreateDialog with AlertDialog.Builder?

I have a button on activity on which I am opening my custom dialog, by calling this method

public void openHcoDialog(View v) {
        HcoDialog hcoDiag = new HcoDialog();
        // Supply cityCode input as an argument.
        Bundle args = new Bundle();
        args.putString("cityCode", cityCode);
        hcoDiag.setArguments(args);
        hcoDiag.show(getSupportFragmentManager(), "hco dialog");
    }

And inside this HcoDialog class I have extended extends DialogFragment as

public class HcoDialog extends DialogFragment {

@NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.hco_dialog, null);
        builder.setView(view);
        builder.setCancelable(true);

        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                Toast.makeText(getActivity(), "Toast 1", Toast.LENGTH_LONG).show();
                progressDialog.dismiss();
            }
        });

        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                Toast.makeText(getActivity(), "Toast 2", Toast.LENGTH_LONG).show();
                progressDialog.dismiss();
            }
        });

}

So when I close dialog box, by clicking outside or pressing back button, Dialog box closes but progressDialog keeps running as onDismiss or onCancel is never called ?

I am trying to resolve this for hour's now. Have read many stackoverflow answer's,but none seem to work.

Thanks in advance.

Upvotes: 1

Views: 1969

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200020

As per the onCreateDialog() documentation:

Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself. To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).

So you must move your logic to the onCancel() and onDismiss() methods on DialogFragment.

Upvotes: 6

Related Questions