FragmentA启动FragmentB,做一些选择操作后,返回FragmentA,需要把FragmentB里面选择的数据传回来。有什么办法?

Fragment之间不能直接通信,必须通过Activity来完成,具体步骤。

1. 在FragmentA中定义通信接口,通过该接口向Activity发送数据。

public class FragmentA extends Fragment {
private onButtonPressListener mListener; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_linmo_select_beitie, container, false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mListener.onOKButtonPressed(selectedBeitie);
}
}); return view;
} @Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (onButtonPressListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onOkButtonPressed");
}
} public interface onButtonPressListener {
void onOKButtonPressed(LinmoBeitieItem item);
}
}

2. 在Activity中实现该接口,并通过该接口向FragmentB传递数据。

public class MainActivity extends Activity implements FragmentA.onButtonPressListener {
@Override
public void onOKButtonPressed(LinmoBeitieItem item) {
FragmentB fragmentB = (FragmentB)getFragmentManager().findFragmentById(R.id.container);
fragmentB.onBeitieSelected(item);
}
}

3. FragmentB接收到数据并处理。

public class FragmentB extends Fragment {
public void onBeitieSelected(LinmoBeitieItem item) {
// ...
}
}

==

两个Fragment之间如何传递数据的更多相关文章

  1. activity与fragment之间的传递数据

    首先activity之间的数据传递就是 用intent和intent+bundle intent 传递 Intent i= new Intent(MainActivity.this,TheAty.cl ...

  2. Android消息机制之实现两个不同线程之间相互传递数据相互调用

    目的:实现两个不同线程之间相互传递数据相互调用方法. 线程一中定义mainHandler 并定义一个方法mainDecode 线程二中定义twoHandler 并定义一个方法twoEncode 实现当 ...

  3. 核心基础以及Fragment与Activity传递数据完整示例

    MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...

  4. Fragment与Activity传递数据

    MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...

  5. 在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程。

    在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程. 答案:可以通过Intent对象.静态变量.剪切板和全局对象进行数据传递,具体的数据传递方法如下. 1. ...

  6. fragment之间相互传数据、共享数据

    在 Fragment 之间共享数据 Activity 中的两个或更多 Fragment 需要相互通信是一种很常见的现象.想象一下拆分视图 (master-detail) Fragment 的常见情况, ...

  7. android78 Fragment和Activity 传递数据

    Activity: package com.itheima.senddata; import android.os.Bundle; import android.app.Activity; impor ...

  8. 两个NetSuite之间历史交易数据迁移的具体方案

    背景与展望: 比如:公司要上市往往会要求提供过去几年的营业数据和报表等信息, 而这些信息来源于正在一直运营使用的ERP和财务系统是最可靠与真实的. NetSuite实现的ERP和财务系统的完美结合,随 ...

  9. 两个Fragment之间传递数据

    1.第一个Fragment BlankFragment blankFragment = new BlankFragment();Bundle bundle = new Bundle();bundle. ...

随机推荐

  1. rsync排除文件同步

    排除扩展名为log的文件 rsync -ave ssh --exclude '*.log' root@192.168.168.188:/website/abc.com/* /website/abc.c ...

  2. the fifth class

      1.实际比背景长,怎么做到的? 2个父级一个做头背景一个做尾背景 2.2层,每次自带背景上下是覆盖关系,如何做到 2层?,子浮动 3.标签 4.border可覆盖:margin-bottom 为负 ...

  3. c# GridView有关RowClick事件,可单击显示选中的row

    //当前选定行 int i = this.gridView1.FocusedRowHandle; //选中行,列名为name的值 gridView1.GetRowCellDisplayText(i, ...

  4. oracle权限语句大全

    Oracle 系统默认的几个用户: sys --------网络管理用户,具有最高数据库管理权限 system------本地管理用户,权限次于sys scott-------普通用户,默认是锁住的( ...

  5. 【洛谷P3385】模板-负环

    这道题普通的bfs spfa或者ballen ford会T 所以我们使用dfs spfa 原因在于,bfs sfpa中每个节点的入队次数不定,退出操作不及时,而dfs则不会 既然,我们需要找负环,那么 ...

  6. tomcat详情

    [转载]http://grass51.blog.51cto.com/4356355/1123400

  7. 【分块打表】bzoj1662 [Usaco2006 Nov]Round Numbers 圆环数

    #include<cstdio> using namespace std; #define BN 380000 const int table[]={0,185815,378154,561 ...

  8. Oracle 服务手动启动关闭

    在windows7中安装完Oracle11g之后会出现一下七种服务:可通过运行->services.msc查看. 其中各个服务名称中的ORCL或orcl为SID即System IDentifie ...

  9. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  10. [UCSD白板题] Least Common Multiple

    Problem Introduction The least common multiple of two positive integers \(a\) and \(b\) is the least ...