如何将结果从第二个片段传递到第一个片段 [英] How to pass result from second fragment to first fragment

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

问题描述

在我的应用程序中,我有两个片段,分别是 fragmentAFragmentB.当我单击 fragmetA 中的按钮时,会在 fragmentB 中打开一个列表.现在,当我从 fragmentB 的列表中选择一个项目时,我希望将结果传递给 fragmentA.我只对所有片段使用一个 TabActivity.当在 fragmentB 中选择列表项时,我从堆栈中弹出 fragmentB 以便我可以直接返回到 fragmentA.

有谁知道如何将结果传递给上一个片段.

谢谢.

解决方案

更新

Activity 是父

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

让活动对其片段应用更改比直接在片段之间传递值更好.让您的 Activity 使用 onQuery(Bundle data)onResult(Bundle data) 方法实现一个 FragmentListener 接口.

在每个片段中创建一个 FragmentListener 变量,然后将每个片段的 onAttach() 重写为:

 public void onAttach(Activity activity) {super.onAttach(活动);//--- 为事件注册父活动---尝试{fragmentListener = (FragmentListener) 活动;}catch (ClassCastException e){throw new ClassCastException("父活动必须实现接口 FragmentListener.");}}

这将强制您的子片段自动注册到父 Activity.

另外,记得在onDetach()中释放fragmentListener引用.

现在您可以从片段调用您的 Activity.

另一方面,您的 Activity 始终可以使用 getFragmentManager().findFragmentByTag("fragmentA")findFragmentById("FragmentA") 搜索片段.如果它可以找到你的 Fragment,那么它就可以将它转换到你的 FragmentA 类中并调用它的方法.同样可以用 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.

Does anyone knows how to pass result to previous fragment.

Thanks.

解决方案

Update

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.

Old

From this tutorial : http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

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.

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.

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

Now you can call your Activity from fragments.

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天全站免登陆