問題描述
我有一個活動可以在運行時顯示不同的對話框.我使用 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 中關閉所有對話框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!