Injeniero Barsa
Injeniero Barsa

Reputation: 1185

Android Dialog dismisses instead of cancel

I'm having the following issue developing in android 2.2 (API 8):

I have a customized Dialog class like this:

public AuthDialog(final Context context, OnDismissListener dismissListener, OnCancelListener cancelListener) {
    super(context);
    setOnDismissListener(dismissListener);
    setOnCancelListener(cancelListener);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userpassdialog);
    setTitle("Enter email and password");
    setCancelable(true);
    setCanceledOnTouchOutside(true);
    authEmail = (EditText) findViewById(R.id.authEmail);
    authPass = (EditText) findViewById(R.id.authPass);
    alertMessage = (TextView) findViewById(R.id.auth_alert);
    Button authButton = (Button) findViewById(R.id.authButton);
    View.OnClickListener onClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            if (checkCredentials())
                dismiss();
            else
                showAlert();
        }
    };
    authButton.setOnClickListener(onClickListener);
}

private void showAlert() {
    alertMessage.setText("Wrong user/pass");
    authEmail.setText(null);
    authPass.setText(null);
}

private boolean checkCredentials() {
    // Empty user/pass for now
    boolean checkEmail = authEmail.getText().toString().equals("");
    boolean checkPassword = authPass.getText().toString().equals("");
    return checkEmail && checkPassword;
}

@Override
public void onBackPressed() {
    cancel();
}

And I create a new AuthDialog like this:

private void authenticateThenAccept() {
    OnDismissListener dismissListener = new OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            accept();
        }
    };
    OnCancelListener cancelListener = new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            cancel();
        }
    };
    AuthDialog dialog = new AuthDialog(context, dismissListener, cancelListener);
    dialog.show();
}

I'm using the debugger, and I see that when I cancel (using the back button or pressing outside the dialog) the app dismisses the dialog instead of cancelling.

Anybody has had this kind of issue with Dialogs?

Thanks in advanced.

Upvotes: 1

Views: 6577

Answers (3)

Kalle
Kalle

Reputation: 2257

onDismiss() is always fired when dialog closes. The documentation for setOnCancelListener() states: "This will only be invoked when the dialog is canceled, if the creator needs to know when it is dismissed in general, use setOnDismissListener", i.e. it's not either onCancel or onDismiss but both when a dialog is canceled. I agree though that it would have made more sense had that not been the case.

Upvotes: 10

Mike dg
Mike dg

Reputation: 4658

setCancelable(false) will prevent the back button from doing anything. Many developers just turn off the ability of the back button to close the dialog since it's unclear whether that is a cancel or ok action to the user.

Upvotes: 0

Paul Nikonowicz
Paul Nikonowicz

Reputation: 3903

Assuming this dialog should be modal, make your dialog a new activity.

Upvotes: 2

Related Questions