我会直言不讳。对话混淆了我的痛苦。为什么?因为似乎有5种不同的方式来实例化它们,给它们一个自定义布局,并使用它们。为了增加对伤害的侮辱,他们的文件非常差。所以我要发布我在这里的主要问题,并希望你们能为我清除一些困惑。
Question One:
what is the Real Difference between Dialog, and DialogFragment?
Question Two:
why is it better to use onCreateView, rather than onCreateDialog?
furthermore, Whats the difference?
Question Three:
why not just do *Dialog dialog = new Dialog();* everytime i need one,
rather than subclass DialogFragment constantly?
如果这个帖子看起来不适合社区,我很抱歉,但请记住这些是非常真实的,未回答的问题。在我读过的所有教程中,从slidenerd到vogella-to-Stack,所有内容都解释了How,但不是为什么,这一点同样重要,如果不是更多的话。谢谢伙计们!
答案 0 :(得分:1)
它们之间的区别在于Dialog只能显示一个自定义视图,但它本身没有办法让你在运行时以编程方式与它的视图/小部件进行交互对话框(即显示时)。 DialogFragment扩展了Fragment并具有Fragment的所有功能和生命周期(当用作对话框时非常类似于它们)。 DialogFragment也可以用作正常的'片段,这意味着您可以使用DialogFragment在平板电脑上显示对话框,或将其置于手机上的活动(即全屏)内。
如果DialogFragment也用作片段,它必须通过onCreateView返回一个View,所以你必须要实现onCreateView。如果只实现onCreateView而不是onCreateView 和 onCreateDialog,则可以避免重复代码。但我从来没有听过有人说使用onCreateView更好,而不是onCreateDialog'。
对于大多数对话框,您不必为DialogFragment创建子类。要显示一个简单的对话框,例如。询问用户显示一些信息的问题,您可以实例化一个新的Dialog()或使用DialogBuilder。只有当Dialog中有一些逻辑在我想要驻留在它自己的类中并使用DialogFragments生命周期时,我才使用DialogFragments。