Dialog.show()vs. Activity.showDialog() [英] Dialog.show() vs. Activity.showDialog()

查看:132
本文介绍了Dialog.show()vs. Activity.showDialog()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,有两种从活动中显示对话框的方式。

As far as I can tell, there are two ways to show a Dialog from an Activity.


  1. 创建对话框,使用 AlertDialog.Builder ),然后调用新创建的对话框的 show()方法。

  2. 调用Activity的 showDialog()方法,传入一个唯一定义要构建什么样的对话框的int。然后覆盖 onCreateDialog()来实际构建对话框,Android将显示给您。

  1. Create the Dialog (for example, using an AlertDialog.Builder), and then call the newly created Dialog's show() method.
  2. Call the Activity's showDialog() method, passing in an int that uniquely defines what sort of Dialog you want to build. Then override onCreateDialog() 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


  1. 如果您需要以某种方式参数化对话框,使用 Activity.showDialog 可能会有点尴尬,如此问题所述。您可能必须在成员变量中存储一个字符串或某些东西,以便稍后在 onCreateDialog onPrepareDialog

  2. 创建和修改对话框的逻辑分布在多个地方,可能使代码更难阅读和维护:


    • 你调用的地方 showDialog()

    • 在一个潜在的大$ switch 语句在被覆盖的 onCreateDialog 方法

    • 在一个潜在的大的在重载的 onPrepareDialog 方法之间切换语句

  1. 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 during onCreateDialog or onPrepareDialog.
  2. 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 overridden onCreateDialog method
    • Inside a potentially large switch statement in the overridden onPrepareDialog method

使用原因 Activity.showDialog


  1. API文档为 Activity.showDialog 表示对话框是由Activity管理的,我认为提供了一些好处?但是,如果您使用 AlertDialog.Builder ,我想,因为您将 作为参考Builder的构造函数。

  2. 如果您的活动要多次显示相同(或非常类似的)对话框,则此选项仅创建一次,而不是每次创建一个新对象,从而减少应变在系统上为新对象分配空间,垃圾回收等。

  1. 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 the AlertDialog.Builder, I would think, because you pass in this as an argument to the Builder's constructor.
  2. 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()vs. Activity.showDialog()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆