如何从第二个片段首先片段通过结果 [英] How to pass result from second fragment to first fragment

查看:126
本文介绍了如何从第二个片段首先片段通过结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我有两个片段说的碎裂 FragmentB 。当我点击一个按钮 fragmetA 的列表,在打开的 fragmentB 。现在,当我从列表中选择一个项目的 fragmentB 我想要的结果将被传递到碎裂。我使用的唯一一个对所有碎片TabActivity。当列表项中选择 fragmentB 我飞出的 fragmentB 从堆栈,这样我就可以直接回去碎裂

In my app I have two fragments say fragmentA and FragmentB. When I click on a button in fragmetA, a list is opened in fragmentB. Now when I select an item from list in fragmentB I want the result to be passed to fragmentA. I am using only one TabActivity for all fragments. When list item is selected in fragmentB I am popping out fragmentB from stack so that I can directly go back to fragmentA.

有谁知道怎么传导致至previous片段。

Does anyone knows how to pass result to previous fragment.

感谢。

推荐答案

更新

活动是父控制器并应处理那些由它的碎片/意见引发的事件,它关注的一些片段的范围之外的责任/视图本身。

Activity is the parent controller and should take responsibility for handling those events raised by its fragments/views, which concern something outside of the scope of fragment/view itself.

一个片段是作为一个分控器意见它承载。所有自己的看法之间的活动和交流,该片段应处理本身。当存在的片段的范围和责任以外的事件(如发送数据到另一片段),即事件应该被提升到它的父控制器,活动

A Fragment is to act as a sub-controller of Views it hosts. All the events and communication between its own views, the fragment should handle itself. When there is an event outside of a fragment's scope and responsibilities (like sending data to another fragment), that event should be escalated to its parent controller, the Activity.

本教程:的http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

其更好地让活动将更改应用到其片段,片段之间直接传递值。让你的活动实现与 onQuery(包数据)一个 FragmentListener 接口 onResult(捆绑数据)的方法。

Its better to let the activity apply changes to its fragment than passing values directly between fragments. Let your Activity implement a FragmentListener interface with onQuery(Bundle data) and onResult(Bundle data) methods.

创建一个 FragmentListener varaible在您的每一个片段,然后覆盖 onAttach()每个片段如下:

Create a FragmentListener varaible in each of your fragments and then override onAttach() of each fragment as:

 public void onAttach(Activity activity) {
    super.onAttach(activity);

    //---register parent activity for events---
    try{
        fragmentListener = (FragmentListener) activity;
    }catch (ClassCastException e)
    {
        throw new ClassCastException("Parent activity must implement interface FragmentListener.");
    }
  }

这将强制您的孩子碎片自动注册到父活动。

This will enforce your child fragments to be automatically registered to parent Activity.

另外,记得要释放 fragmentListener 参照 onDetach()

Also, remember to release fragmentListener reference in onDetach().

现在你可以从片段打电话给你的活动。

Now you can call your Activity from fragments.

在另一边,你的活动可以随时搜索使用片段 getFragmentManager()。findFragmentByTag(碎裂) findFragmentById(碎裂)。 如果能够找到你的片段,然后,它可以转换成你的碎裂类,并调用它的方法。同样可以用 FragmentB 完成或任何其他片段。

On the other side, your Activity can always search for a fragment using getFragmentManager().findFragmentByTag("fragmentA") or findFragmentById("FragmentA"). If it can find your Fragment, Then it can cast it into your FragmentA class and call its methods. Same can be done with FragmentB or any other fragment..

这篇关于如何从第二个片段首先片段通过结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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