1. Activity 给 Fragment传值
    1.1 Activity中代码
//send data to fragment        Bundle bundle = new Bundle();        bundle.putString("landId", landId);        pictureDisplayFragment.setArguments(bundle);
1.2 Fragment 接受代码
Bundle bundle = getArguments();        String landId = bundle.getString("landId");
  1. Fragment 给所附属的 Activity传值

    2.1 首先在 fragment 中写回调接口和重写onAttach方法,并创建实例化

//实例化接口private CallBackValue mCallBackValue;//定义一个回调接口      public interface CallBackValue{          public void SendBundle(Bundle bundle);      }    //fragment 与 activity 通过onAttach() 方法回调,重写该方法即可@Override      public void onAttach(Activity activity) {          // TODO Auto-generated method stub          super.onAttach(activity);          //当前fragment从activity重写了回调接口得到接口的实例化对象          mCallBackValue = (CallBackValue) getActivity();      }  
2.2 实现接口和回调后,Fragment 通过下面的代码向 Activity 传值。
Intent picPlayActivity = new Intent(getActivity(), PicturePlayActivity.class);Bundle bundle = new Bundle();bundle.putString("siteName","地块名");mCallBackValue.SendBundle(bundle );
2.3 Activity 中要继承 Fragment中实现的回调接口
implements CallBackValue
2.4 Activity 通过下面代码接受传值
 @Override      public void SendBundle(Bundle bundle) {      //这里只是举例子,接收的参数的一般是外部变量        String string = bundle.getString("siteName");    } 
至此,Fragment 给 Activity 传值就完成了。
  1. Fragment 给另外的 Activity 传值

    3.1 Fragment 中实现代码

   Intent picPlayActivity = new Intent(getActivity(), PicturePlayActivity.class);           Bundle toActivity = new Bundle();        toActivity.putString("siteName","地块名");        picPlayActivity.putExtras(toActivity);                startActivity(picPlayActivity);
3.2 Activity 中接受值代码
Intent intent = getIntent();        Bundle bundle = intent.getExtras();String siteName = bundle.getString("siteName");
很简单就完成了。

更多相关文章

  1. Android JNI 实例: SimpleJNI
  2. Android 监听按钮点击事件的三种方式(1:匿名内部类;2:外部类;3:接口方
  3. Android代码总结,Sdcard和图片相关
  4. android 下载不了源代码?
  5. android 2.2 完全 退出 ---程序代码
  6. Android 功能代码总结

随机推荐

  1. PL/SQL中复制中文再粘贴出现乱码问题的解
  2. python数据存储系列教程——python中mysq
  3. linux下mysql相关命令
  4. MongoDB备份恢复与导入导出
  5. mysql 数据库备份和还原 实践
  6. 面试39 MySQL读写分离
  7. 优惠券使用限制的数据库设计
  8. 如何用sql语言只获得数据库当前日期,且格
  9. MemSQL Start[c]UP 2.0 - Round 1 E - Th
  10. PHP获取MySQL执行sql语句的查询时间