問題描述
據我所知,有兩種方法可以從 Activity 中顯示 Dialog.
As far as I can tell, there are two ways to show a Dialog from an Activity.
- 創建 Dialog(例如,使用
AlertDialog.Builder
),然后調用新創建的 Dialog 的show()
方法. - 調用 Activity 的
showDialog()
方法,傳入一個 int,它唯一地定義了您想要構建的 Dialog 類型.然后重寫onCreateDialog()
以實際構建 Dialog,Android 將為您顯示它.
- Create the Dialog (for example, using an
AlertDialog.Builder
), and then call the newly created Dialog'sshow()
method. - Call the Activity's
showDialog()
method, passing in an int that uniquely defines what sort of Dialog you want to build. Then overrideonCreateDialog()
to actually build the Dialog, and Android will display it for you.
第二種方法似乎是標準做法,但我很好奇我使用哪種方法是否重要.以下是我能想到的:
The second method seems to be the standard practice but I'm curious if there is any reason it matters which one I use. Here's all I can come up with:
使用Dialog.show
- 如果您需要以某種方式參數化 Dialog,使用
Activity.showDialog
可能會有點尷尬,如 這個問題.您可能必須在成員變量中存儲字符串或其他內容,以便稍后在onCreateDialog
或onPrepareDialog
期間檢索它. - 創建和修改對話框的邏輯分布在多個地方,可能會使代碼更難閱讀和維護:
- 你調用
showDialog()
的地方 - 在被覆蓋的
onCreateDialog
方法中可能很大的switch
語句中 - 在被覆蓋的
onPrepareDialog
方法中可能很大的switch
語句中
- 你調用
- If you need to parameterize the Dialog in some way, it can be a little awkward to use
Activity.showDialog
, as described in this question. You may have to store a String or something in a member variable, just so that it can be retrieved moments later duringonCreateDialog
oronPrepareDialog
. - The logic for creating and modifying the dialog is spread out across a number of places, potentially making the code harder to read and maintain:
- The place where you call
showDialog()
- Inside a potentially large
switch
statement in the overriddenonCreateDialog
method - Inside a potentially large
switch
statement in the overriddenonPrepareDialog
method
- The place where you call
使用Activity.showDialog
的原因:
Activity.showDialog
的 API 文檔說 Dialog 是由 Activity 管理"的,我想這會帶來一些好處嗎?但如果你使用AlertDialog.Builder
也是如此,我認為,因為你將this
作為參數傳遞給 Builder 的構造函數.- 如果您的 Activity 將多次顯示相同(或非常相似)的 Dialog,則此選項只創建一次,而不是每次都創建一個新的,從而減少系統分配空間的壓力用于新對象、垃圾回收等.
- The API docs for
Activity.showDialog
say that the Dialog is "managed" by the Activity which I suppose provides some benefit? But this is also true if you use theAlertDialog.Builder
, I would think, because you pass inthis
as an argument to the Builder's constructor. - If your Activity is going to show the same (or a very similar) Dialog several times, this option creates it only once, instead of creating a new one each time, thus putting less strain on the system as far as allocating space for new objects, garbage collection, etc.
所以我的問題是,決定何時使用Activity.showDialog
和何時使用Dialog.show
的標準是什么,為什么?
So my question is, what are the criteria for deciding when to use Activity.showDialog
and when to use Dialog.show
, and why?
推薦答案
在我看來你應該更喜歡 showDialog
因為這個方法會為你完成大部分工作.例如,您不必擔心更改屏幕方向后會丟失對對話框的引用.它將自動重新創建.Dialog.show
更容易出錯.
In my opinion you should prefer showDialog
because this method will do most of the work for you. In example You don't have to worry that you will lose reference to your dialog after changing screen orientation. It will be recreated automatically. Dialog.show
is much more prone to errors.
所以我建議你盡可能使用 showDialog
.
So I suggest you to use showDialog
everywhere you can.
這篇關于Dialog.show() 與 Activity.showDialog()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!