我们是否应该使用setTargetFragment()?我以为片段不应该互相通信 [英] Should we be using setTargetFragment()? I thought Fragments should not be communicating with each other

查看:1085
本文介绍了我们是否应该使用setTargetFragment()?我以为片段不应该互相通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android开发教程推荐我使用的片段主机活动,以绕过数据和诸如此类的东西,那么,为什么会出现一组/获取目的片段的方法?

The android developer tutorials recommend me using the host activities of fragments to pass around data and whatnot, so why is there a set/get target fragment method?

我的应用程序迄今包含一个主机活动,和片段,其中有一个按钮,启动一个 DialogFragment ,其中有一个按钮,启动另一个 DialogFragment 。使用 setTargetFragment / getTargetFragment 使得这整个的考验有点混乱了,所以我想重新实现,让我的主要活动手柄 DialogFragment.show 方法,通过我的主要片段的自定义界面。

My application thus far contains a host activity, and a fragment, which has a button that launches a DialogFragment, in which there is a button that launches ANOTHER DialogFragment. Using setTargetFragment/getTargetFragment has made this whole ordeal somewhat confusing though, so I am thinking of reimplementing to let my main activity handle DialogFragment.show methods through my main fragment's custom interface.

这是思想的正确方法吗?或者有没有使用任何伤害 setTargetFragment ?任何人都可以提供使用它的好和坏的例子?

Is this the right way of thinking? Or is there no harm in using setTargetFragment? Can anyone provide good and bad examples of using it?

推荐答案

我不认为这是在使用setTargetFragment隐性的伤害,但是,我只会在非常特殊的情况下使用。例如,如果目标片段的只有的打算有史以来片段中使用(考虑对象重用和设计类是可重复使用的可能时),即使这样,有节制地。

I don't think there is implicit harm in using setTargetFragment, however, I would only use it in very specific circumstances. For example, if the target fragment is only going to ever be used by the fragment (taking into account object reuse and designing your classes to be reusable when possible) and even then, sparingly.

通过使用他们太多,你最终会与你所看到的 - 混乱code,是很难跟踪和维护。在一开始,通过您的活动编组一切你保持一个平的层次结构是简单的遵循和维护。

By using them too much, you will end up with what you're seeing - confusing code that is hard to follow and maintain. On the outset, by marshaling everything through your activity you maintain a "flat" hierarchy that is simple to follow and maintain.

我觉得用setTargetFragment与否的决定是一种编码式/哲学的一个,用智慧和经验,感觉对还是错。也许在你的情况下,有证据证明,你质疑你的旧的code,你获得的智慧:)

I think the decision to use setTargetFragment or not is a coding-style/philosophical one that, with wisdom and experience, it "feels" right or wrong. Maybe on your case, by evidence that you are questioning your older code, you are gaining that wisdom :)

这篇关于我们是否应该使用setTargetFragment()?我以为片段不应该互相通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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