先说说背景知识:

(From:http://blog.csdn.net/t12x3456/article/details/8119607

尽管fragment的实现是独立于activity的,可以被用于多个activity,但是每个activity所包含的是同一个fragment的不同的实例。
Fragment可以调用getActivity()方法很容易的得到它所在的activity的对象,然后就可以查找activity中的控件们(findViewById())。例如:
ViewlistView =getActivity().findViewById(R.id.list);同样的,activity也可以通过FragmentManager的方法查找它所包含的frament们。

有时,你可能需要fragment与activity共享事件。一个好办法是在fragment中定义一个回调接口,然后在activity中实现之。
例如,还是那个新闻程序的例子,它有一个activity,activity中含有两个fragment。fragmentA显示新闻标题,fragmentB显示标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,然后activity再告诉fragmentB,fragmentB就显示出对应的内容(为什么这么麻烦?直接fragmentA告诉fragmentB不就行了?也可以啊,但是你的fragment就减少了可重用的能力。现在我只需把我的事件告诉宿主,由宿主决定如何处置,这样是不是重用性更好呢?)。


好,原理就是这些,下面是我自己的一个例子。

程序入口类(MainAcivity)继承自FragmentActivity,包含了两个Fragment。第一个FragmentA放一个EditText,第二个FragmentB中嵌入高德的地图MapView。

要实现的功能是在FrangmentB中进行定位,然后把定位得到的经纬度坐标通过MainAcivity传给FragmentA,最终显示在FragmentA的EditText上。

第一步,在FragmentB中定义一个接口,监听定位:

public class FragmentB extends android.app.Fragment {

    //Container Activity must implement this interface
public interface OnLocationGotListener { //监听定位是否完成
public void onLocationGot(LatLonPoint latLonPoint);
}
......

然后通过onAttach方法,在MainActivity建立的时候,检查其是否实现了OnLocationGotListener 接口,并对传入的Activity的实例进行类型转换,如下

public class FragmentB extends android.app.Fragment {

    OnLocationGotListener onLocationGotListener;

    public interface OnLocationGotListener {
public void onLocationGot(LatLonPoint latLonPoint);
} @Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
onLocationGotListener = (OnLocationGotListener)activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ "must implement onLocationGotListener");
}
}
......

然后在FragmentB的相关函数里为接口里定义的方法传值,如下:

  /**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation aLocation) {
if (mListener != null && aLocation != null) {
mListener.onLocationChanged(aLocation);// 显示系统小蓝点
android.util.Log.i("activate","success"); LatLonPoint noteLatLonPoint = new LatLonPoint(aLocation.getLatitude(),aLocation.getLongitude());
onLocationGotListener.onLocationGot(noteLatLonPoint); //将定位得到的经纬度坐标传递给接口的方法
}
}

第二步,MainActivity中实现接口,并在MainActivity中通过getFragmentManager()获取FragmentA中的EditText,为其重新赋值:

public class MainActivity extends FragmentActivity implements FragmentB.OnLocationGotListener {
@Override
public void onLocationGot(LatLonPoint latLonPoint) {
mLatLonPoint = latLonPoint; // 获取坐标
Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_a); // 获取FragmentA的视图
EditText editText = (EditText)fragment.getView().findViewById(R.id.editText) ; // 获取FragmentA中的editText对象
editText.setText(latLonPoint.toString()); // 给FragmentA中editText赋值
}
}
......

Fragment 与 Activity 通信的更多相关文章

  1. 关于Fragment与Fragment、Activity通信的四种方式

    一直想总结一下Fragment与Fragment.Activity通信的问题,今天有时间一共总结了三种,权当抛砖引玉,如果大家还有更好的方式来实现Fragment和Fragment.Activity的 ...

  2. Android学习——Fragment与Activity通信(二)

    接下来就要到Fragment向Activity传输数据了.主要的思路,就是在Fragment中创建一个回调接口,利用该回调接口实现Fragment向Activity传输数据的功能. 回调函数(接口) ...

  3. Android学习——Fragment与Activity通信(一)

    学会了在Activity中加载Fragment的方法之后,接下来便需要学习Activity和Fragment之间的通信.这一节先学习如何把Activity中的信息传递给Fragment. 基本过程 在 ...

  4. Android 使用EventBus进行Fragment和Activity通信

    本文介绍EventBus的基本使用,以及用于Fragment和Activity之间通信. github地址: https://github.com/greenrobot/EventBus 版本是 Ev ...

  5. fragment 与activity通信 Bundle 序列化数据 Serializable

    1.fragment简单套用(静态调用): 新建一个fragment,其xml文件如下: <LinearLayout xmlns:android="http://schemas.and ...

  6. android中fragment与activity之间通信原理以及例子

    参考文章 http://blog.csdn.net/guozh/article/details/25327685#comments Activity和fragment通信方式一般有3种方法 1.在fr ...

  7. Fragment间的通信

    在网上看到的一篇文章,总结的很好 为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把 ...

  8. Fragment之间的通信

    在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...

  9. Fragment与Acitvity通信

    Fragment与Activity通信的方式如下: 一.通过初始化函数提供 1.在动态添加Fragment的过程中,我们在Activity中通过Fragment.setArguments()的方法为F ...

随机推荐

  1. Linux操作系统启动流程浅析

    启动BIOS程序 当电源键按下之后,BIOS(Basic nput Output System)就会由主板上的闪存来运行.BIOS程序会把自己解压缩到系统的内存之中,然后读取CMOS(Compleme ...

  2. Linux时间相关函数

    相关文件: /etc/localtime  本地时间二级制文件 /etc/sysconfig/clock  时区配置文件 /usr/share/zoneinfo  存储各个时区的二进制文件 时间修改方 ...

  3. 仿UC天气下拉和微信下拉眼睛头部淡入淡出--第三方开源--PullLayout

    Android-PullLayout是github上的一个第三方开源项目,该项目主页是:https://github.com/BlueMor/Android-PullLayout 原作者项目意图实现类 ...

  4. windows创建桌面快捷方式的VBA脚本

    Dim wShell, oShortcut    'Dim strDesktop$ ' 为了与VBS兼容,    Dim strDesktop    ' 这里改写一下,测试通过...    Set w ...

  5. jvm 数据区划分学习

    Java virtual machine 运行时数据存储区域划分 2015年1月25日 19:15 Pc  寄存器 Each Java Virtual Machine thread has its o ...

  6. Oracle定义两个变量,并对两个变量的值的长度进行判断

    这个例子其实很简单,但是往往简单的东西如果不用心就会漏洞百出,简单的一个逻辑判断,是为了给复杂逻辑判断做出铺垫 语法格式: if<condition_expression> then pl ...

  7. mapreduce 实现pagerank

    输入格式: A 1 B,C,D B 1 C,Dmap: B A 1/3 C A 1/3 D A 1/3 A |B,C,D C B 1/2 D B 1/2 B |C,Dreduce: B (1-0.85 ...

  8. Jboss wildfly add JDBC driver

    Jboss wildfly  添加 JDBC driver 我这里使用的是 wildfly-8.0.0.Final 第一步: 首先在modules里面添加mysql的驱动包 例如:modules\sy ...

  9. iOS10和Xcode8适配

    1 Xib文件的注意事项 使用Xcode8打开xib文件后,会出现下图的提示. 大家选择Choose Device即可. 之后大家会发现布局啊,frame乱了,只需要更新一下frame即可.如下图 注 ...

  10. 复习JS和jQuery

    复习JS和jQuery 近些时日,以前学过的东西忘了好多.今天且写一点复习一下JS和jQuery.希冀与某年某月某日,忘却的时候,能看一下自己写的博文,尚可记起一二. 现在有需求如下:有两个按钮,一个 ...