在AlertDialog DialogFragment优势优势、AlertDialog、DialogFragment

2023-09-03 22:11:54 作者:从头酷到脚

当开发一个Android应用程序,我读过,它建议使用的 DialogFragment 不是直接使用的 AlertDialog 显示提醒和确认。

When developing an Android app, I've read that it's recommended to use DialogFragment instead of using directly an AlertDialog to show alerts and confirmations.

这是完成的,例如,在DialogFragment的文档:http://developer.android.com/reference/android/app/DialogFragment.html

This is done, for example, on DialogFragment's Documentation: http://developer.android.com/reference/android/app/DialogFragment.html

人还表示,他们preFER这个位置: 的Andr​​oid DialogFragment VS对话

People also say they prefer this here: Android DialogFragment vs Dialog

我想知道这种方法的优点,因为code变得更加复杂。

I would like to know the advantages of this approach, since the code becomes more complex.

感谢

推荐答案

自推出API级别的13

的showDialog从活动方法的德precated 的。 在调用code对话框其他地方是不可取的,因为你将不得不管理自己的对话框(例如,方向变化)。不使用ShowDialog的会导致偶然的例外,该对话框不会链接到任何活动。

the showDialog method from Activity is deprecated. Invoking a dialog elsewhere in code is not advisable since you will have to manage the the dialog yourself (e.g. orientation change). Not using the showDialog will result in occasional exceptions, the dialog is not linked to any Activity.

请注意有关的ShowDialog:

Note about showDialog:

对话框的参考:活动都提供了管理创建,保存和恢复对话框。见onCreateDialog(INT),在prepareDialog(INT,对话)的ShowDialog(INT),和dismissDialog(INT)。如果使用这些方法,getOwnerActivity()将返回管理该对话框的活动。

reference of Dialog: Activities provide a facility to manage the creation, saving and restoring of dialogs. See onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int), and dismissDialog(int). If these methods are used, getOwnerActivity() will return the Activity that managed this dialog.

差异DialogFragment - AlertDialog

这是读你的问题的时候想到的一件事。难道他们这么多有什么不同? 一个 DialogFragment 是类似于一个对话框pretty的,它只是一个包裹里面的片段。从Android的参考有关DialogFragment:

One thing that comes to mind when reading your question. Are they so much different? A DialogFragment is pretty similar to an Dialog, it's just wrapped inside a fragment. From Android reference regarding DialogFragment:

一个DialogFragment是显示一个对话框窗口,浮在上面的片段其   活动的窗口。这个片段含有一个对话对象,它   适当显示基于该片段的状态。的控制   对话框(决定何时显示,隐藏,关闭它)应该做的   通过这里的API ,不与对话框上直接调用。

A DialogFragment is a fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

其他笔记

片段在Android框架的自然发展,由于不同屏幕尺寸的设备的多样性。 DialogFragments和片段都在支持库,这使得该类Android中的所有当前使用的版本可用的提供。