Fragment与Activity的通信(回调),Fragment间的通信
来源:互联网 发布:网络seo 编辑:程序博客网 时间:2024/06/08 16:13
一、消息Fragment-->Activity
1、Fragment启动Activity时通过Intent将数据传递过去,这种方法每次都要重启Activity。
2、通过回调方法:
2.1 普通的回调方法。
Fragment类中定义方法switch:
private void switch(Fragment f) {if(f != null){if(getActivity() instanceof MainActivity){((MainActivity)getActivity()).switchFragment(f);}}}该方法MainActivity 的实例,实现调用MainActivity 中的方法实现通信。
MainActivity类中对应的回调方法switchFragment(Fragment f)完成响应。
2.2 通过接口实现回调:
2.2.1 在Fragment类中定义接口及抽象方法,并在onAttach方法中添加如下代码:
private OnFragmentChangeListener mCallBack;public interface OnFragmentChangeListener {public void onTabSwitch(int position);}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);//确保包含Fragment的Activity已经实现了回调接口,否则抛出异常try {mCallBack = (OnFragmentChangeListener) activity;} catch (Exception e) {throw new ClassCastException(activity.toString()+ " must implement OnFragmentChangeListener");}}
2.2.2 在Activity中实现上面的接口:
public class TestActivity extends Activity implementsMyFragment.OnFragmentChangeListener {@Overridepublic void onTabSwitch(int position) {//此处接收事件的回调}}
2.2.3 在Fragment将信息发送给父Activity:
mCallBack.onTabSwitch(pos);
二、消息从Activity-->Fragment
1、通过实例化一个Fragment
在Activity中设置如下代码携带参数传递给Fragment:
Fragment2 newFragment = new Fragment2();Bundle args = new Bundle();args.putInt(Fragment2.ARG_KEY, position);newFragment.setArguments(args);getFragmentManager().beginTransaction().replace(R.id.frame, newFragment).commit();
或者通过构造方法将数据传递给Fragment(官方不推荐):
Fragment2 newFragment = new Fragment2(arg1,arg2);
推荐做法:
1.1在Fragment中
public static MyFragment newInstance(String userName, String password) {MyFragment frgmt = new MyFragment();Bundle args = new Bundle();args.putString("userName", userName);args.putString("password", password);frgmt.setArguments(args);return frgmt;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (null != getArguments()) {String userName = getArguments().getString("userName");String password = getArguments().getString("password");}}
1.2Activity中调用:
MyFragment myFragment = MyFragment.newInstance("myName", "myPsw");
2、通过findFragmentById()或者findFragmentByTag()方法找到Fragment的实例:
MyFragment fragment = (MyFragment) getFragmentManager().findFragmentById(R.id.frame);if(fragment!=null){fragment.frgmtFunc();}
参考自:http://blog.csdn.net/xyz_lmn/article/details/8631195#t1
0 0
- Fragment与Activity的通信(回调),Fragment间的通信
- Fragment与Activity的通信:
- Fragment与Activity的通信
- Fragment与Activity的通信
- activity间的通信,fragment间的通信,fragment与activity间的通信
- Activity与Fragment,Fragment与Fragment之间的通信
- fragment与activity间的通信(一)
- fragment与activity间的通信(二)
- Fragment与Activity之间的通信
- android Activity与Fragment之间的通信
- fragment与activity之间的通信
- Activity与Fragment之间的通信详解
- Fragment与Activity的相互通信(一)
- Fragment与Activity的相互通信(二)
- Activity与Fragment的通信方式
- Activity与Fragment之间的通信
- Fragment与Activity之间的相互通信
- Fragment与Activity之间的通信
- leetcode Permutations
- poj 1028 False coin
- alter table move跟shrink space的区别
- Fedora开启ssh服务
- 1、Extjs——ExtJs简介
- Fragment与Activity的通信(回调),Fragment间的通信
- siege 压力测试
- socket阻塞与非阻塞,同步与异步、I/O模型
- asp.net中的按钮在页面上有验证控件的时候不触发按钮事件
- 【Unix/Linux】【命令】【文本工具】文件/输出分页显示——more
- 四 序列容器
- 上班==任务
- 关于NIO2的relativize的关键词使用
- html的swf文件代码解析