Dialog.show() 与 Activity.showDialog()

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

问题描述

据我所知,有两种方法可以从 Activity 中显示 Dialog.

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

  1. 创建 Dialog(例如,使用 AlertDialog.Builder),然后调用新创建的 Dialog 的 show() 方法.
  2. 调用 Activity 的 showDialog() 方法,传入一个 int,它唯一地定义了您想要构建的 Dialog 类型.然后重写 onCreateDialog() 以实际构建 Dialog,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. 如果您需要以某种方式参数化 Dialog,使用 Activity.showDialog 可能会有点尴尬,如 这个问题.您可能必须在成员变量中存储字符串或其他内容,以便稍后在 onCreateDialogonPrepareDialog 期间检索它.
  2. 创建和修改对话框的逻辑分布在多个地方,可能会使代码更难阅读和维护:
    • 你调用showDialog()
    • 的地方
    • 在被覆盖的 onCreateDialog 方法中可能很大的 switch 语句中
    • 在被覆盖的 onPrepareDialog 方法中可能很大的 switch 语句中
  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. Activity.showDialog 的 API 文档说 Dialog 是由 Activity 管理"的,我想这会带来一些好处吗?但如果你使用 AlertDialog.Builder 也是如此,我认为,因为你将 this 作为参数传递给 Builder 的构造函数.
  2. 如果您的 Activity 将多次显示相同(或非常相似)的 Dialog,则此选项只创建一次,而不是每次都创建一个新的,从而减少系统分配空间的压力用于新对象、垃圾回收等.
  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() 与 Activity.showDialog()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)