从DialogFragment接收结果 [英] Receive result from DialogFragment

查看:186
本文介绍了从DialogFragment接收结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DialogFragments了一些事情:从列表中选择项目,输入文本。

I am using DialogFragments for a number of things: choosing item from list, entering text.

什么是返回一个值的最佳方法(即字符串或从列表中的项目)返回给调用活动/片段?

What is the best way to return a value (i.e. a string or an item from a list) back to the calling activity/fragment?

目前我正在调用的活动实施DismissListener并给予DialogFragment引用该活动。对话框然后调用活动中的OnDimiss方法和活动抓住从DialogFragment对象的结果。非常混乱,并没有对配置变化(方向的改变)作为DialogFragment工作失去了参考的活动。

Currently I am making the calling activity implement DismissListener and giving the DialogFragment a reference to the activity. The Dialog then calls the OnDimiss method in the activity and the activity grabs the result from the DialogFragment object. Very messy and it doesn't work on configuration change (orientation change) as the DialogFragment loses the reference to the activity.

感谢您的帮助。

推荐答案

使用 myDialogFragment.setTargetFragment(这一点,MY_REQUEST_ code)从您展示的地方对话框,然后在完成你的对话框,从中你可以调用 getTargetFragment()。onActivityResult(getTargetRequest code(),...)和实施 onActivityResult()包含片段。

Use myDialogFragment.setTargetFragment(this, MY_REQUEST_CODE) from the place where you show the dialog, and then when your dialog is finished, from it you can call getTargetFragment().onActivityResult(getTargetRequestCode(), ...), and implement onActivityResult() in the containing fragment.

看起来 onActivityResult()的滥用,特别是因为它根本不涉及活动。但我已经看到了它的建议谷歌官方的人,甚至在API演示。我认为这是什么克/ setTargetFragment()添加的。

It seems like an abuse of onActivityResult(), especially as it doesn't involve activities at all. But I've seen it recommended by official google people, and maybe even in the api demos. I think it's what g/setTargetFragment() were added for.

这篇关于从DialogFragment接收结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆