Fragment与Activity的通信(回调),Fragment间的通信

一、消息Fragment-->Activity

1、Fragment启动Activity时通过Intent将数据传递过去,这种方法每次都要重启Activity。

2、通过回调方法:

  2.1 普通的回调方法。

 
Fragment类中定义方法switch:
[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. private void switch(Fragment f) {  
  2.     if(f != null){  
  3.         if(getActivity() instanceof MainActivity){  
  4.             ((MainActivity)getActivity()).switchFragment(f);  
  5.         }  
  6.     }  
  7. }  
       该方法MainActivity 的实例,实现调用MainActivity 中的方法实现通信。
       MainActivity类中对应的回调方法switchFragment(Fragment f)完成响应。
 

  2.2 通过接口实现回调:

 

    2.2.1 在Fragment类中定义接口及抽象方法,并在onAttach方法中添加如下代码:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. private OnFragmentChangeListener mCallBack;  
  2.   
  3. public interface OnFragmentChangeListener {  
  4.     public void onTabSwitch(int position);  
  5. }  
  6.   
  7. @Override  
  8. public void onAttach(Activity activity) {  
  9.     super.onAttach(activity);  
  10.     //确保包含Fragment的Activity已经实现了回调接口,否则抛出异常  
  11.     try {  
  12.         mCallBack = (OnFragmentChangeListener) activity;  
  13.     } catch (Exception e) {  
  14.         throw new ClassCastException(activity.toString()  
  15.                     + " must implement OnFragmentChangeListener");  
  16.     }  
  17. }  

    2.2.2 在Activity中实现上面的接口:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. public class TestActivity extends Activity implements  
  2.         MyFragment.OnFragmentChangeListener {  
  3.     @Override  
  4.     public void onTabSwitch(int position) {  
  5.         //此处接收事件的回调  
  6.     }  
  7. }  

    2.2.3 在Fragment将信息发送给父Activity:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. mCallBack.onTabSwitch(pos);  
 
      两种回调方法都很适用于通过Fragment切换Activity的页面。
 

二、消息从Activity-->Fragment

1、通过实例化一个Fragment

    在Activity中设置如下代码携带参数传递给Fragment:
[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. Fragment2 newFragment = new Fragment2();  
  2. Bundle args = new Bundle();  
  3. args.putInt(Fragment2.ARG_KEY, position);  
  4. newFragment.setArguments(args);  
  5. getFragmentManager().beginTransaction().replace(R.id.frame, newFragment).commit();  

   或者通过构造方法将数据传递给Fragment(官方不推荐):
[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. Fragment2 newFragment = new Fragment2(arg1,arg2);  
 
推荐做法:

1.1在Fragment中

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. public static MyFragment newInstance(String userName, String password) {  
  2.         MyFragment frgmt = new MyFragment();  
  3.         Bundle args = new Bundle();  
  4.         args.putString("userName", userName);  
  5.         args.putString("password", password);  
  6.         frgmt.setArguments(args);  
  7.         return frgmt;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         if (null != getArguments()) {  
  14.             String userName = getArguments().getString("userName");  
  15.             String password = getArguments().getString("password");  
  16.         }  
  17.     }  


1.2Activity中调用:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. MyFragment myFragment = MyFragment.newInstance("myName", "myPsw");  
 
 

2、通过findFragmentById()或者findFragmentByTag()方法找到Fragment的实例:

 
[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. MyFragment fragment = (MyFragment) getFragmentManager().findFragmentById(R.id.frame);  
  2. if(fragment!=null){  
  3.     fragment.frgmtFunc();  
  4. }  

参考自:http://blog.csdn.net/xyz_lmn/article/details/8631195#t1
posted @ 2016-12-30 15:28  天涯海角路  阅读(205)  评论(0)    收藏  举报