gpt4 book ai didi

重新创建 Activity 后,Android Dialog 再次显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:38 24 4
gpt4 key购买 nike

我有一个 Activity ,需要在其中显示对话框。这里一切正常。我已经重写了 Activity 中的 onCreateDialog 方法,这里是代码:

@Override
protected Dialog onCreateDialog(int dialog)
{
if(dialog == 10)
{
if(waitDialog != null)
waitDialog.dismiss();

dialogCreated = true;
waitDialog = CreateWaitDialog(this);
return waitDialog;
}
else
return new Dialog(this);
}

其中 CreateWaitDialog 是创建对话框的自定义方法,而 waitDialog 是静态变量。

我通过调用 showDialog(10) 显示对话框

所有代码都运行良好。

显示对话框后,我将通过调用将其关闭。

if(waitDialog != null)
waitDialog.hide();

当 Activity 被销毁时,我将关闭它。

if(dialogCreated)
dismissDialog(10);
super.onDestroy();

它正在关闭,一切都很好。但是,当我改变设备的方向并重新创建 Activity 时,它会自己再次弹出!我没有调用任何 showDialog 或类似的东西,它只是弹出!

最佳答案

我认为这是 Activity 和 onCreateDialog 的定义行为:

Callback for creating dialogs that are managed (saved and restored) for you by the activity. The default implementation calls through to onCreateDialog(int) for compatibility. If you are targeting HONEYCOMB or later, consider instead using a DialogFragment instead.

If you use showDialog(int), the activity will call through to this method the first time, and hang onto it thereafter. Any dialog that is created by this method will automatically be saved and restored for you, including whether it is showing.

If you would like the activity to manage saving and restoring dialogs for you, you should override this method and handle any ids that are passed to showDialog(int).

我猜测 onDestroy 在 Activity 生命周期中为时已晚,无法关闭对话框。我的猜测是 Activity 将您的对话框保存在 onSaveInstanceState 中。

我可能会在调用 super.onSaveInstanceState 之前尝试关闭 onSaveInstanceState 中的对话框,然后该对话框将在 android 尝试保存和恢复它之前关闭。

@Override
onSaveInstanceState(Bundle outstate) {

dismissDialog(10);
super.onSaveInstanceState(outstate);
}

关于重新创建 Activity 后,Android Dialog 再次显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13055997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com