Android DialogFragment 与 Dialog
Google 建议我们通过使用 Fragments API
使用 DialogFragment
而不是简单的 Dialog
,但是使用孤立的 DialogFragment< /code> 用于简单的是-否确认消息框。在这种情况下,最佳做法是什么?
Google recommends that we use DialogFragment
instead of a simple Dialog
by using Fragments API
, but it is absurd to use an isolated DialogFragment
for a simple Yes-No confirmation message box. What is the best practice in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
是的,使用
DialogFragment
并在onCreateDialog
中,您可以简单地使用 AlertDialog 构建器来创建一个带有“是/否”确认按钮的简单AlertDialog
。根本没有太多代码。关于处理片段中的事件,有多种方法可以实现,但我只是在我的
Fragment
中定义一个消息Handler
,将其传递到DialogFragment 通过其构造函数,然后根据各种单击事件将消息传递回我的片段的处理程序。同样有多种方法可以做到这一点,但以下方法对我有用。
在对话框中保存一条消息并在构造函数中实例化它:
在对话框中实现
onClickListener
,然后根据需要调用处理程序:Edit
并作为
Message 是可打包的,您可以将其保存在
onSaveInstanceState
中并恢复它,然后在
onCreate
中Yes, use
DialogFragment
and inonCreateDialog
you can simply use an AlertDialog builder anyway to create a simpleAlertDialog
with Yes/No confirmation buttons. Not very much code at all.With regards handling events in your fragment there would be various ways of doing it but I simply define a message
Handler
in myFragment
, pass it into theDialogFragment
via its constructor and then pass messages back to my fragment's handler as approprirate on the various click events. Again various ways of doing that but the following works for me.In the dialog hold a message and instantiate it in the constructor:
Implement the
onClickListener
in your dialog and then call the handler as appropriate:Edit
And as
Message
is parcelable you can save it out inonSaveInstanceState
and restore itThen in
onCreate
您可以创建通用的 DialogFragment 子类,例如 YesNoDialog 和 OkDialog,如果您在应用程序中经常使用对话框,则可以传入标题和消息。
然后使用以下代码调用它:
并在
onActivityResult
中处理结果。You can create generic DialogFragment subclasses like YesNoDialog and OkDialog, and pass in title and message if you use dialogs a lot in your app.
Then call it using the following:
And handle the result in
onActivityResult
.使用 DialogFragment 而不是 AlertDialog:
自 API 级别 13 推出以来:
showDialog 方法活动已被弃用。
不建议在代码中的其他位置调用对话框,因为您必须自己管理对话框(例如方向更改)。
差异DialogFragment - AlertDialog
它们有那么大的不同吗?来自关于 DialogFragment 的 Android 参考:
<块引用>
DialogFragment 是一个显示对话框窗口的片段,浮动在其顶部
活动的窗口。该片段包含一个 Dialog 对象,它
根据片段的状态适当显示。控制
应该完成对话框(决定何时显示、隐藏、关闭它)
通过此处的 API,而不是直接调用对话框。
其他说明
Use DialogFragment over AlertDialog:
Since the introduction of API level 13:
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).
Difference DialogFragment - AlertDialog
Are they so much different? From Android reference regarding DialogFragment:
Other notes
我建议使用
DialogFragment
。当然,考虑到它应该是相当简单的任务,用它创建一个“是/否”对话框是相当复杂的,但用
Dialog
创建一个类似的对话框也非常复杂。(活动生命周期使其变得复杂 - 您必须让
Activity
管理对话框的生命周期 - 并且无法将自定义参数(例如自定义消息)传递给Activity.showDialog
如果使用低于 8 的 API 级别)好的一点是,您通常可以非常轻松地在
DialogFragment
之上构建自己的抽象。I would recommend using
DialogFragment
.Sure, creating a "Yes/No" dialog with it is pretty complex considering that it should be rather simple task, but creating a similar dialog box with
Dialog
is surprisingly complicated as well.(Activity lifecycle makes it complicated - you must let
Activity
manage the lifecycle of the dialog box - and there is no way to pass custom parameters e.g. the custom message toActivity.showDialog
if using API levels under 8)The nice thing is that you can usually build your own abstraction on top of
DialogFragment
pretty easily.具有生成器模式的通用 AlertDialogFragment
在我的项目中,在我发现它有问题之前,我已经使用了
AlertDialog.Builder
很多。但是,我不想在应用程序中的任何地方更改那么多代码。此外,我实际上喜欢在需要时将 OnClickListeners 作为匿名类传递(即使用 setPositiveButton()、setNegativeButton() 时) > 等),而不必实现数千个回调方法来在对话框片段和持有者片段之间进行通信,在我看来,这可能会导致非常混乱和复杂的代码。特别是,如果一个片段中有多个不同的对话框,然后需要在回调实现中区分当前显示的对话框。因此,我结合了不同的方法来创建一个通用的
AlertDialogFragment
帮助器类,它的使用方式完全像AlertDialog
:解决方案
(请注意,我在代码中使用了 Java 8 lambda 表达式,因此,如果您不使用 lambda 表达式 尚未。)
用法
我在这里发布此内容不仅是为了分享我的解决方案,也是因为我想征求大家的意见:这种方法合法还是在某种程度上有问题?
Generic AlertDialogFragment with Builder Pattern
In my project, I already used
AlertDialog.Builder
already a lot before I found out that it's problematic. However, I did not want to change that much code anywhere in my app. Additionally, I actually am a fan of passingOnClickListeners
as anonymous classes where they are needed (that is, when usingsetPositiveButton()
,setNegativeButton()
etc.) instead of having to implement thousands of callback methods to communicate between a dialog fragment and the holder fragment, which can, in my opinion, lead to very confusing and complex code. Especially, if you have multiple different dialogs in one fragment and then need to distinguish in the callback implementations between which dialog currently being shown.Therefore, I combined different approaches to create a generic
AlertDialogFragment
helper class which can be used exactly likeAlertDialog
:SOLUTION
(PLEASE NOTE that I am using Java 8 lambda expressions in my code, so you might have to change parts of the code if you are not using lambda expressions yet.)
USAGE
I am posting this here not only to share my solution, but also because I wanted to ask you people for your opinion: Is this approach legit or problematic to some extent?
DialogFragment 基本上是一个可以用作对话框的 Fragment。
更多详细信息
DialogFragment is basically a Fragment that can be used as a dialog.
More detail
我可以建议对@ashishduh的答案进行一些简化:
它消除了用户(类的)熟悉组件内部的需要,并使使用变得非常简单:
PS在我的例子中,我需要一个简单的警报对话框,所以这就是我创造了什么。您可以将该方法应用于是/否或您需要的任何其他类型。
May I suggest a little simplification of @ashishduh's answer:
It removes the need for the user (of the class) to be familiar with the internals of the component and makes usage really simple:
P.S. In my case I needed a simple alert dialog so that's what I created. You can apply the approach to a Yes/No or any other type you need.
使用 Dialog 进行简单的是或否对话框。
当您需要更复杂的视图(需要掌握生命周期,例如 oncreate、请求权限、任何生命周期覆盖)时,我将使用对话框片段。因此,您可以将权限与对话框运行所需的任何其他代码分开,而无需与调用活动进行通信。
Use Dialog for simple yes or no dialogs.
When you need more complex views in which you need get hold of the lifecycle such as oncreate, request permissions, any life cycle override I would use a dialog fragment. Thus you separate the permissions and any other code the dialog needs to operate without having to communicate with the calling activity.
对话框:对话框是一个小窗口,提示用户做出决定或输入附加信息。
DialogFragment: DialogFragment 是一个特殊的片段子类,旨在创建和托管对话框。它允许 FragmentManager 管理对话框的状态并在发生配置更改时自动恢复对话框。
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information.
DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs. It allows the FragmentManager to manage the state of the dialog and automatically restore the dialog when a configuration change occurs.
DialogFragment 具有对话框和片段的功能。基本上所有生命周期事件都可以通过 DialogFragment 自动管理,例如屏幕配置的更改等。
DialogFragment comes with the power of a dialog and a Fragment. Basically all the lifecycle events are managed very well with DialogFragment automatically, like change in screen configuration etc.