就是为什么不推荐片段之间的直接沟通? [英] Why direct communication between fragments is not recommended?

查看:108
本文介绍了就是为什么不推荐片段之间的直接沟通?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习上<一款Android碎片href="http://developer.android.com/training/basics/fragments/communicating.html">developer.android.com这是指定的两个片段之间的直接通信,不推荐。我想知道会有什么后果的案件中,两个片段之间的沟通会失败?

while learning android fragments on developer.android.com it is specified that direct communication between two fragment is not recommended. I want to know what will be the consequences and the cases in which communication between two Fragments would fail?

推荐答案

那么,与片段你并不总是知道他们一定会健在,并连接在通信时。无论片段连接并可用与否也可能取决于设备的布局和大小。如果你是绝对相信你的片段都将被连接到你的活动,可在同一时间,那么我想你可以直接通信。

Well, with Fragments you aren't always sure if they will be alive and attached at the time of communication. Whether Fragments are attached and available or not might also depend on device layout or size. If you're absolutely sure that your Fragments will both be attached to your activity and available at the same time, then I suppose you can communicate directly.

说了这么多,片段,目的是要合乎逻辑的,独立的单位。从文档:

Having said that, Fragments are meant to be logical, standalone units. From the docs:

You can think of a fragment as a modular section of an activity

这样的突破,如果碎片直接互相影响的型号。

It kind of breaks the model if the fragments are affecting each other directly.

为什么不情愿定义一个接口在你的活动,并得到片段A调用在活动的方法?那么你的活动可以检查片段B是否可用,然后可以调用相应的功能片段B。

Why not rather define an interface in your Activity and get Fragment A to call a method in the Activity? Then your Activity can check whether Fragment B is available and can then call the appropriate function in Fragment B.

这里是文档的建议

这篇关于就是为什么不推荐片段之间的直接沟通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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