先说说背景知识:

(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. Code Review的一些典型内容

    如下是Code Review中一些典型的内容: 一.常规项: 1.代码能够工作么?它有没有实现预期的功能,逻辑是否正确等. 2.所有的代码是否简单易懂? 3.代码符合你所遵循的编程规范么?这通常包括大 ...

  2. 《shell条件测试语句,字符串测试apache是否开启》

    还得我想了10分钟才明白”!=“和"-n"的用法区别,做个笔记捋一捋 第一种方法:测试apache是否开启?字符串测试 #!/bin/bash web=`/usr/bin/pgre ...

  3. ThreadLocal学习记录

    ThreadLocal简介 当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的 ...

  4. 将word中的“空格” 转换为换行符

  5. 转换 Html 内容为纯文本内容(html,文本互转)

    转自http://www.cnblogs.com/jyshi/archive/2011/08/09/2132762.html : /// <summary> /// 转换纯文本内容为 HT ...

  6. Oracle 10g 数据文件的第一个数据块结构

    一.数据文件的第一个数据块结构kcvfh BBED> set file 1 FILE# 1 BBED> set block 1 BLOCK# 1 --查看第一个数据块的整体结构 BBED& ...

  7. SQLite简易入门

    本文内容来源:https://www.dataquest.io/mission/129/introduction-to-sql 本文所用数据来源:https://github.com/fivethir ...

  8. 解决ASP.NET使用IIS架设网站时“服务器应用程序不可用”的方法

    服务器应用程序不可用您试图在此 Web 服务器上访问的 Web 应用程序当前不可用.请点击 Web 浏览器中的“刷新”按钮重试您的请求. 管理员注意事项: 详述此特定请求失败原因的错误消息可在 Web ...

  9. Ubuntu下codeblocks汉化

    code::blocks是一个十分好用编辑环境,一个在手,无所不能,为了更好的支持中文,我列出了汉化的方法: 1下载中文汉化包:http://pan.baidu.com/s/1hqvNZbI 2.解压 ...

  10. ASP.NET MVC 学习第二天

    今天使用mvc完成简单的增删改,内容比较简单,来熟悉一下mvc,数据库操作是用前面的ef,也算是温习一下ef吧. 新建mvc项目,在项目中的Models内添加ef,我这里只操作一下简单的user表.里 ...