最佳答案
我有一些片段需要显示一个常规的对话框。在这些对话框中,用户可以选择“是”或“否”,然后片段就应该有相应的行为。
现在,Fragment
类没有要重写的 onCreateDialog()
方法,所以我想我必须在包含 Activity
的外部实现对话框。没关系,但是 Activity
需要以某种方式向片段报告所选择的答案。当然,我可以在这里使用回调模式,这样片段就会在 Activity
中用侦听器类注册自己,Activity 就会通过侦听器类或类似的东西来报告答案。
但是,对于在片段中显示“简单”的 yes-no 对话框这样的简单任务来说,这似乎是一个相当大的混乱。而且,这样我的 Fragment
就不那么独立了。
有什么更干净的方法吗?
编辑:
这个问题的答案并没有详细解释应该如何使用 DialogFragments 来显示片段中的对话框。所以 AFAIK,方法是:
.setTargetFragment()
将原始片段设置为此 DialogFragment 的目标。