活动与互动片段 [英] Activity And Fragment Interaction

查看:181
本文介绍了活动与互动片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动有多个片段秒。我想说明一个 DialogFragment 或打开另一个片段片段之一秒。我知道,一个活动应该是一个与开放的任务片段如此,而不是我已经尝试了几件事情。

I have an Activity with multiple Fragments. I want to show a DialogFragment or open another Fragment from one of the Fragments. I know that an Activity should be the one tasked with opening Fragments so instead I have tried a couple things.

第一个结果
我试图用 getActivity()丢,所以我可以调用活动的方法来显示片段然而,这会在片段活动依赖和我想避免添加如果可能的依赖。搜索结果
第二个结果
接下来,我尝试了听众通知活动,它应该表现出片段。所以,我在活动创建一个类来实现侦听器接口。但我有问题,因为我不得不使用新MyActivity()新侦听器(); ,它会抛出一个例外当我试图用 getSupportFragmentManager()因为活动的这个实例没有初始化。结果

第三结果
然后,我试过有活动实施直接听众其中工程因为那时我只创建一个与听众,而不是活动的依赖性。不过我现在越来越严重,我的活动将实施2点 - 这是让我犹豫,因为它会严重降低凝聚力4种不同的界面搜索结果

FIRST
I tried to use getActivity() and cast it so I can call a method in the Activity to show a Fragment however this creates a dependency in the Fragment with the Activity and I would like to avoid adding a dependency if possible.

SECOND
Next I tried a listener to notify the Activity that it should show a Fragment. So I created a class in the Activity to implement the listener interface. But I had problems because I had to use New MyActivity().new Listener(); and it would throw an Exception when I tried to use getSupportFragmentManager() since this instance of the Activity is not initialized.

THIRD
I then tried to have the Activity implement the listener directly which works because then I am only creating a dependency with the listener and not the Activity. However now I am getting to the point where my Activity will be implementing 2 - 4 different interfaces which is making me hesitant because it will severely reduce cohesion.

所以,我曾尝试任何办法,我似乎遇到了一个砖墙和创建的依赖关系我不知道我需要被创造。我拧,不得不去与这些选项之一?如果是这样的选项将是最好的?任何帮助或建议,都大大AP preciated。

So any way I have tried I seem to be running into a brick wall and creating dependancies I'm not sure I need to be creating. Am I screwed and have to go with one of these options? If so which option would be best? Any help or suggestions are are greatly appreciated.

推荐答案

我个人会说,碎片应该被认为是为可重用的和模块化的组件。因此,为了提供这种重复使用性,碎片应该不是很了解他们的父母的活动。但换来的活动,必须了解他们持有片段。

Personally I would say that fragments should be thought as reusable and modular components. So in order to provide this re-usability, fragments shouldn't know much about their parent activities. But in return activities must know about fragments they are holding.

所以,第一个选项不应该在我关于你提到的造成了非常高度耦合的code依赖的原因意见考虑。

So, the first option should never be considered in my opinion for the dependency reason you mentioned causing a very highly coupled code.

关于第二个选项,片段可将任何应用程序流量或用户界面相关的决定(呈现出新的片段,决定何时被触发特定片段事件等做什么..)到其父活动。所以,你的听众/回调应该是具体片段的,因此它们应该在片段中声明。而持有这些片段应该实现这些接口,并决定该怎么做。活动

About the second option, fragments may delegate any application flow or UI related decisions (showing a new fragment, deciding what to do when a fragment specific event is triggered etc..) to their parent activities. So your listeners/callbacks should be fragment specific and thus they should be declared in fragments. And the activities holding these fragments should implement these interfaces and decide what to do.

所以对我的第三个选择更有意义。我认为活动是什么,他们都抱着和做具体的回调方面更具可读性。但是,是的你是对你的活动可能会成为神的对象。

So for me the third option makes more sense. I believe that activities are more readable in terms of what they are holding and doing on specific callbacks. But yes you are right your activity might become a god object.

也许你可以检查广场的奥托的项目,如果你不想要实现多个接口。它基本上是一个事件总线。

Maybe you can check Square's Otto project if you don't want to implement several interfaces. It's basically an event bus.

这篇关于活动与互动片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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