我有一个活动可以在运行时显示不同的对话框.我使用 onCreateDialog(int id) 创建每个对话框,并使用 showDialog(int id) 和 dismissDialog(int id) 方法分别显示和关闭每个对话框.
I have an activity that could show different dialogs during run-time. I use onCreateDialog(int id) to create each dialog and I use showDialog(int id) and dismissDialog(int id) method show and dismiss each dialog respectively.
当 onPause() 被调用时,我不知道正在显示哪个对话框(如果有的话).我想确保在调用 onPause 时,所有对话框都被关闭.有没有推荐的方法来关闭所有对话框?我是否必须为每个对话框调用dismissDialog()?
When onPause() is called, I don't know which dialog (if any) is being displayed. I want to make sure that when onPause is called, all dialogs are dimissed. Is there a recommended way to dismiss all dialogs? Would I have to call dismissDialog() for each dialog?
取决于我们正在谈论的对话框数量.简短的回答是肯定的,您必须关闭每个对话框.
Depending on how many dialog's we're talking about. The short answer is yes, you'll have to dismiss each dialog.
除了简单地在活动级别声明一些对话框之外,可能还有其他优雅的方法可以做到这一点.一旦声明它们,您可以将所有对话框存储在 HashMap 中,然后遍历列表并在暂停时关闭每个对话框.
There may be elegant ways of doing this other than simply having a few dialogs declared at the activity level. You could store all the dialogs in a HashMap once they are declared and then iterate through the list and close each one onPause.
由于您不知道哪些是开放的,您需要通过并测试或跟踪状态.
Since you don't know which are open you'll need to go through and test or track the states.
但是,如果您的屏幕上确实有这么多对话框,则您的 UI/UX 设计可能会出现一些问题,因为 Android 应该为您提供一个模型,该模型可以轻松实现它,而不会出现看似糟糕的设计.
However, if you truly have this many dialogs on your screen you may have some issues with your UI/UX design as Android should give you a model which makes it easy to implement it without what seems like poor design.
这篇关于Android - 如何在 onPause 中关闭所有对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!