user1555863
user1555863

Reputation: 2607

Android AlertDialog onDismiss get user choice

Consider the following AlertDialog:

pizzaOrderDialog = new AlertDialog.Builder(mContext);
    pizzaOrderDialog.setTitle("One last thing:");
    pizzaOrderDialog.setMessage("Would you like black or green olives?")
            .setCancelable(true)
            .setPositiveButton("Black", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    orderPizza("black");
                    Toast.makeText(mContext, "Ok! Thanks!", Toast.LENGTH_LONG).show();
                }
            })
            .setNegativeButton("Green", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    orderPizza("green");
                    Toast.makeText(mContext, "Ok! Thanks!", Toast.LENGTH_LONG).show();
                }
            });
    alertD = pizzaOrderDialog.create();

It's possible that the user also chooses to dismiss the dialog by touching anywhere on the screen, not clicking "Black" or "Green". What I want is to "catch" this case and get an indication of this scenario in onDismiss:

alertD.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //TODO tell if dialog result was dialogInterface.BUTTON_NEGATIVE
            // or maybe dialogInterface.BUTTON_POSITIVE ?
        }
    });

What's the best practice here? Thanks!

Upvotes: 0

Views: 64

Answers (1)

user1555863
user1555863

Reputation: 2607

Obviously, as pointed out by @CommonsWare, I needed to use setOnCancelListener here.

Upvotes: 1

Related Questions