Android 旋转屏幕时取消警报对话框
我有一个AlertDialog.Builder显示在应用程序上。当我旋转屏幕时,会出现“应用程序已泄漏窗口”错误。如何取消onPause()事件中的AlertDialog?我没有看到Ad.cancel()的任何方法 更新:为工作模型编辑的代码Android 旋转屏幕时取消警报对话框,android,android-alertdialog,Android,Android Alertdialog,我有一个AlertDialog.Builder显示在应用程序上。当我旋转屏幕时,会出现“应用程序已泄漏窗口”错误。如何取消onPause()事件中的AlertDialog?我没有看到Ad.cancel()的任何方法 更新:为工作模型编辑的代码 private AlertDialog inputDlg; ... @Override protected void onPause () { // close dialog if it's showing
private AlertDialog inputDlg;
...
@Override
protected void onPause ()
{
// close dialog if it's showing
if (inputDlg != null)
if (inputDlg.isShowing ())
inputDlg.dismiss ();
super.onPause ();
}
...
private void dlgUserPrompt (Context c)
{
// Set an EditText view to get user input
final EditText input = new EditText (c);
AlertDialog.Builder Ab = new AlertDialog.Builder (this);
Ab.setTitle("title");
Ab.setMessage("I won't explode if you rotate this");
Ab.setView(input);
inputDlg = Ab.show ();
}
您需要重写
onConfigurationChange()
hook来完成此操作。这将有助于向您详细解释配置更改的确切含义以及如何处理它们。您需要覆盖onConfigurationChange()
钩子来完成此操作。这将有助于向您详细解释配置更改的确切含义以及如何处理这些更改。我认为您正在寻找方法。我认为您正在寻找方法。实际上,在使用AlertDialog.Builder时,您不会得到简单的方法。在阅读了Kurtis的回复后,看起来解决方案实际上相当复杂。AlertDialog.Builder上的show()方法返回一个AlertDialog对象,该对象确实有一个disclose方法。除非您在视图中执行的是计算密集型操作,否则最好让Android在配置更改时终止/重新启动您的活动,而不是在清单中覆盖它,因为这是一个更简单的解决方案。实际上,在使用AlertDialog.Builder时,您不会得到简单的关闭方法。在阅读了Kurtis的回复后,看起来解决方案实际上相当复杂。AlertDialog.Builder上的show()方法返回一个AlertDialog对象,该对象确实有一个disclose方法。除非您在视图中做的是计算密集型的事情,否则最好让Android在配置更改时终止/重新启动您的活动,而不是在清单中覆盖它,因为这是一个更简单的解决方案。