片段创建后片段间通信 [英] Inter fragment communication after fragment creation

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

问题描述

在我Acitivty我继续根据用户数据fragements之间切换。

In my Acitivty i keep on switching between the fragements depending upon the user data.

现在可以说我有两个片段A和B点。

Now lets say i have two Fragments A & B.

现在我想片段与B通信。

Now i want fragment A to communicate with B.

当某些条件片段可以称之为主办的活动,以取代B片段当前片段(与计划作为工作提交操作)。

Upon some condition fragment might call the hosted activity to replace the current fragment with fragment B (with the commit operation which is scheduled as a work ).

现在我想一些沟通,从断枝一个发生断枝B(从打电话断枝一个断枝上B A法)
但FRAG B的方法需要的frag B的整个创作过程发生,我不知道什么时候会发生,因为这是异步发生。

Now i want some communication to happen from frag A to frag B (call a method on frag b from frag A) But that method of frag B requires the whole creation process of frag B to happen which i don't know when will happen because that is happening asynchronously.

我如何能取得这样的场景,其中一个的frag应有助于箱的frag B,然后在断枝乙调用一个方法。

How can i achieve such scenario where frag A should help to crate frag B and then invoke a method on frag B.

我已经检查了片段间的通信话题 http://developer.android。 COM /培训/基础知识/片段/ communicating.html 但更多的是对如何更换一个片段。对于如何经过沟通可能发生没有资料。

I have checked the inter fragment communication topic http://developer.android.com/training/basics/fragments/communicating.html but that is more on how to replace a fragment. There is no info on how after that communication can happen.

欢呼声,
Saurav

cheers, Saurav

推荐答案

那么你总是可以去你的活动 getActivity(),询问是否有你想要一个片段与 findFragmentByXyz()来沟通,用它做你的东西(或创建它,如果它尚未创建)。

Well you could always go to your Activity getActivity(), ask if there is a Fragment you want to communicate with findFragmentByXyz() and do your stuff with it (or create it, if it's not created yet).

如果你想确保,你与其他片段沟通,当它准备(例如,当它的UI已经准备好),你可以在片段B的UI发布可运行,并通知您活动/片段A /什么的,当你准备好了。

If you want to make sure, that you communicate with the other Fragment, when it's ready (e.g. when its UI is ready), you can post a 'Runnable' on the UI of Fragment B and notify your Activity/Fragment A/whatever, when you're ready.

getView().post(new Runnable() {
    @Override
    public void run() {
        if (getActivity() != null) { // or whatever
             registerComponents();
        }
    }
});

这篇关于片段创建后片段间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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