我们如何知道用户究竟是如何取消对话的?

How do we know how exactly user cancelled the dialog?

我在 setCanceledOnTouchOutside(true) 中使用了一个可取消的对话框片段。现在我要区分两种情况:

这两个操作都会导致调用 onCancel()onDismiss() 侦听器。此外,这两个侦听器接受 DialogInterface 作为参数,因此没有任何 "event state" 来检查用户究竟有多 cancelled 对话。

那我该怎么办?

要知道用户是否按下了设备的后退按钮,重写 onBackPressed 方法:

@Override
public void onBackPressed()
{
 //here you could set a boolean to know if the user pressed the back button, and react accordingly when the dialog is closed.
backPressed=true;
}

例如