I am showing dialog on activity start with:
mDialog.setCanceledOnTouchOutside(false);
When user press the back button, its first dismiss the dialog and then on again press on back button it close the activity. I want to do this in single back press, close the dialog and close activity. I have tried wit the following code also:
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // AppDialogUtils.mDialog.setCancelable(true);
            // AppDialogUtils.mDialog.dismiss();
            mActivity.finish();
        }
        return super.onKeyDown(keyCode, event);
    }
Set a mDialog.setOnCancelListener(listener) and close the Activity in that listener.
@Override
mDialog.setOnCancelListener(new OnCancelListener() {
    public void onCancel(DialogInterface interface) {
        this.finish();            
    }
});
Alternatively you could use a OnKeyListener for your Dialog.
mDialog.setOnKeyListener(new Dialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface interface, int keyCode,
                KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                this.finish();
            }
            return true;
        }
    });
Use OnCancelListener
alertDialog.setOnCancelListener(new OnCancelListener() {
    public void onCancel(DialogInterface dialog) {
        // finish activity             
    }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With