When dialog.setCanceledOnTouchOutside(true); then you just override onCancel() like this:
dialog.setOnCancelListener(
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//When you touch outside of dialog bounds,
//the dialog gets canceled and this method executes.
}
}
);
Type your code inside the onCancel() method so it runs when the dialog gets canceled.