Android Dialog.show() 与 Activity.showDialog()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3170308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dialog.show() vs. Activity.showDialog()
提问by MatrixFrog
As far as I can tell, there are two ways to show a Dialog from an Activity.
据我所知,有两种方法可以从活动中显示对话框。
- 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.
- 创建 Dialog(例如,使用
AlertDialog.Builder
),然后调用新创建的 Dialog 的show()
方法。 - 调用 Activity 的
showDialog()
方法,传入一个 int ,该 int 唯一定义要构建的对话框类型。然后覆盖onCreateDialog()
以实际构建对话框,Android 将为您显示它。
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:
第二种方法似乎是标准做法,但我很好奇是否有任何原因让我使用哪种方法很重要。这是我能想到的所有内容:
Reasons to use Dialog.show
使用理由 Dialog.show
- 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
- 如果您需要以某种方式对 Dialog 进行参数化,则使用 可能有点尴尬
Activity.showDialog
,如本问题所述。您可能必须在成员变量中存储字符串或其他内容,以便稍后在onCreateDialog
或期间检索它onPrepareDialog
。 - 创建和修改对话框的逻辑分布在多个地方,可能会使代码更难阅读和维护:
- 你打电话的地方
showDialog()
- 在
switch
重写onCreateDialog
方法中的潜在大语句中 - 在
switch
重写onPrepareDialog
方法中的潜在大语句中
- 你打电话的地方
Reasons to use Activity.showDialog
:
使用理由Activity.showDialog
:
- 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.
- API 文档
Activity.showDialog
说 Dialog 是由 Activity “管理”的,我认为这提供了一些好处?但是,如果您使用AlertDialog.Builder
,我认为也是如此,因为您将this
作为参数传入Builder 的构造函数。 - 如果您的 Activity 将多次显示相同(或非常相似)的 Dialog,则此选项只会创建一次,而不是每次都创建一个新的,因此就为新对象分配空间而言,对系统的压力较小、垃圾收集等。
So my question is, what are the criteria for deciding when to use Activity.showDialog
and when to use Dialog.show
, and why?
所以我的问题是,决定何时使用Activity.showDialog
和何时使用的标准是什么Dialog.show
,为什么?
采纳答案by Dariusz Bacinski
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
这种方法,因为这种方法将为您完成大部分工作。例如,您不必担心在更改屏幕方向后会丢失对对话框的引用。它将自动重新创建。Dialog.show
更容易出错。
So I suggest you to use showDialog
everywhere you can.
所以我建议你在showDialog
任何可能的地方使用。
回答by Dheeraj Vepakomma
You can use the overloaded method showDialog(int, Bundle)
introduced in API level 8.
Simple shove the message to be shown into the bundle, which will be available in onPrepareDialog(int, Dialog, Bundle)
.
您可以使用showDialog(int, Bundle)
API 级别 8 中引入的重载方法。
简单地将要显示的消息推送到包中,该包将在onPrepareDialog(int, Dialog, Bundle)
.
Yes, I know that showDialog()
itself has been deprecated now.
是的,我知道它showDialog()
现在已经被弃用了。
回答by Cristian
I think the decision is up to you, depending on the good reasons you have pointed out. If I have an activity which has just one dialog (say, for displaying an alert) I'd just fire the Dialog.show
method... on the other hand, if the UI of my app relies too much of its functionality on the use of dialogs it'd be much better to use showDialog()
. I think the best advantage of using showDialog()
is that it's easier to read:
我认为决定取决于你,这取决于你指出的充分理由。如果我有一个只有一个对话框的活动(例如,用于显示警报),我只会触发该Dialog.show
方法......另一方面,如果我的应用程序的 UI 太多依赖于使用对话框的功能使用showDialog()
. 我认为使用的最大优点showDialog()
是更容易阅读:
final int ERROR_DIALOG = 404;
final int MSG_DIALOG = 200;
.....
// if someone else read this, he/she will immediately understand what's going on
showDialog( ERROR_DIALOG );