安卓:为什么片段之间的沟通应通过活动发生的呢? [英] Android: Why communication between Fragments should happen through Activity?

查看:159
本文介绍了安卓:为什么片段之间的沟通应通过活动发生的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于如何让两个片段使用接口和事件回电相互通信线程通过活性的方法
是否有任何具体的理由这样做呀?或者是有什么缺点做这种方式 - 从片段1片段2直接调用方法

 ((Fragment2)(getActivity()getFragmentManager()findFragmentById(R.id.fragment2))。)methodOfFragment2()。


解决方案

不幸的是很多教程活动接口不给人为真正使他们的片段模块化一个很好的模式。

大多数人最终做的是使他们的活动界面,用于硬编码fragements之间的关系,一些中央存储库。因此,当我第一次开始usnig片断和阅读有关这个​​模特我也想知道,在它的价值,因为我只是在我的片段关系引入了一个间接层。

加班我学会或调整模式,如广播器/接收模式,我的片段。现在我的片段不通过活动的inteface沟通,而是通过当地的广播对象,而不是广播与有效载荷事件频事件,感兴趣的那些事件,其他对象可以注册以接收这些事件和数据。

在换句话说,我的片段具有事件,以及它们发布的那些事件,常与有效载荷,和其它的目的可以注册以从这些事件接收通知和数据。如果没有人听,没有任何反应。如果我写希望的从事件接收数据的一些新的对象,我的其他片段,甚至我的广播不需要知道我的新目标是如何实现的任何细节的。

如果您想了解更多关于广播接收器模型的Andr​​oid都有自己的LocalBroadcastManger,或者你可以写你自己的轻量级之一,你可以找到关于如何在这里使用它的一些教程:

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

如何使用LocalBroadcastManager?

There are lot of threads about how to make two Fragments communicate each other using an interface and event call back methods through Activity. Is there any specific reason to do that way? or is there any downside doing this way-directly calling method of fragment-2 from fragment-1

 ((Fragment2) (getActivity().getFragmentManager().findFragmentById(R.id.fragment2))).methodOfFragment2();

解决方案

Unfortunately the activity-interface in many tutorials doesn't give people a good model for actually making their fragments modular.

What most people end up doing is making their activity-interface some central repository for hardcoding relationships between fragements. Thus, when I first started usnig fragments and read about this "model" I too wondered at its value, since I was just introducing a layer of indirection over my fragment relations.

overtime I learned or adapted patterns, like the Broadcast/Receiver model to my fragments. Now my fragments don't communicate through the activity-inteface, but instead broadcast events on an event-frequency with payloads through a local broadcast object, and other objects interested in those events can register to receive those events and data.

In other words, my fragments have events, and they publish those events, often with payloads, and other objects can register to receive notification and data from those events. If no one's listening, nothing happens. If I write some new object that want's to receive data from the event, my other fragments and even my broadcaster don't need to be aware of any specifics of how my new object was implemented.

If you want to learn more about the broadcast Receiver model Android has its own LocalBroadcastManger, or you can write your own lightweight one, and you can find some tutorials on how to use it here:

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

how to use LocalBroadcastManager?

这篇关于安卓:为什么片段之间的沟通应通过活动发生的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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