Fragment 与 Activity 通信

先说说背景知识:

(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赋值
    }
}
......
原文地址:https://www.cnblogs.com/duanguyuan/p/4006639.html