先说说背景知识:

(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下的vi编辑器与gcc工具的使用

    最近在网上找了些视频,自学了一点Linux环境下,C编程的方法. 注 vi与vim是有区别的,vim打开的源码文件其中的关键字是有颜色的. vi编辑器有3种模式,命令行模式,插入模式,底行模式. 如果 ...

  2. C++ const用法小结 (欢迎大家拍砖)

    C++const 关键字小结 const 是constant的缩写,本意是不变的,不易改变的意思. const 在C++中是用来修饰内置类型变量,自定义对象,成员函数,返回值,函数参数. 一.cons ...

  3. json string 与object 之间的转化

    1.将json string转化成object 1: public static T GetObjectFromJson<T>(string jsonString) 2: { 3: Dat ...

  4. Python开发【第一篇】Python基础之自定义模块和内置模块

    为什么要有模块,将代码归类.模块,用一砣代码实现了某个功能的代码集合. Python中叫模块,其他语言叫类库. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代 ...

  5. selenium+python 浏览器标签页跳转 switch_to_window

    浏览器页面跳转方法记录: from selenium import webdriver import time browser = webdriver.Chrome() first_url='http ...

  6. mysql笔记整理

    删除整个表 TRUNCATE TABLE 表名; 持久链接 自动提交

  7. matlab实现雅可比、高斯塞德尔、后项误差计算

    稀疏矩阵生成: function [a, b] = aparsesetup(n) e = ones(n, 1); n2 = n / 2; a = spdiags([-e 3*e -e], -1:1, ...

  8. auto和decltype

    auto 1.编译器通过分析表达式的类型来确定变量的类型,所以auto定义的变量必须有初始值. auto i=; //ok,i为整型 auto j; //error,定义时必须初始化. j=;     ...

  9. select * from table where 1=1

    转自:http://www.dzwebs.net/2418.html 我们先来看看这个语句的结果:select * from table where 1=1,其中where 1=1,由于1=1永远是成 ...

  10. LintCode-Search 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...