Mysterious_android
Mysterious_android

Reputation: 618

How do I override dismiss() in Dialog ? Android

I want to do some data checking after dialog.dismiss() or dialog.cancel is called.

How should I go about doing this?

Are there any overrides, Listener or methods that i can implement to do some action then dismiss the Dialog?

I tried dialog.setOnDismissListener but that dismisses the dialog first then calls this listener.

Upvotes: 3

Views: 3438

Answers (2)

pumawo
pumawo

Reputation: 11

Hoping it may help if AlertDialog works as Dialog.

I just did this type of coding and got such results:

somewhere in the code:

AlertDialog diaBox = myDialog();
                    diaBox.show();
                    Toast.makeText(getContext(), "this time it is 4", Toast.LENGTH_SHORT).show();

Than making "myDialog()" method:

private AlertDialog myDialog() {
        AlertDialog myQuittingDialogBox = new AlertDialog.Builder(v_context)
                // set message, title, and icon
                .setTitle("Delete")
                .setMessage("Do you want to delete")

                .setPositiveButton("Delete", (dialog, whichButton) -> {
                    //Any code is possible
                    dialog.dismiss();
                })
                .setNegativeButton("Cancel", (dialog, which) -> {
                    //Any code is possible
                    Toast.makeText(getContext(), "this time it is 1", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    Toast.makeText(getContext(), "this time it is 2", Toast.LENGTH_SHORT).show();
                })
                .create();

        myQuittingDialogBox.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                // Custom actions when the dialog is dismissed
                Toast.makeText(getContext(), "this time it is 3", Toast.LENGTH_SHORT).show();
            }
        });

        return myQuittingDialogBox;
    }

And the Toast sequence was like this: 4 Hitting NegativeButton 1 2 3

So everything that I wanted I did in the public void onDismiss method and everything worked fine :)

P.S. If you found mistake or know a better solution - PLEASE! write it down and I will edit and fix my code - THANK YOU!

P.P.S. If you will make dialog.dismiss(); it will always show you that Toast with 3 in it! For now I do not know how to call super .dismiss() so it would not show you Toast with 3 in it. But I may update it later :)

Upvotes: 0

mbo
mbo

Reputation: 4691

You could create your own custom Dialog with a DialogFragment and just overwrite dismiss().

public class CustomDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Build the alert dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles);
        return builder.create();
    }

    @Override
    public void dismiss() {
        // Do your stuff here
        super.dismiss();
    }

    ...
}

Take a look here https://developer.android.com/guide/topics/ui/dialogs.html

Upvotes: 5

Related Questions