Fragment间通信传递数据 Communicating with Other Fragments
来源:互联网 发布:淘宝宝贝详情免费模板 编辑:程序博客网 时间:2024/05/22 04:37
需求:
一个Activity中显示两个Fragment,想要在FragmentA中点击某个按钮时,切换到FragmentB,同时把用户输入的数据传递到B中。
思路:
Fragment的显示与否决定权在Activity里,想要传递数据就得通过这个“媒婆”,两个Fragment不应该直接通信。
方法:(其实这里就是一个回调的概念。)
1。先在FragmentA中定义一个接口,例如:
/** * 注册成功后回调,用于传递数据至登录 */ public interface OnRegisterSuccessListener { void onRegisterSuccess(String phoneNumber); }
2。 然后在A中创建一个OnRegisterSuccessListener接口的对象,在按钮的点击事件里调用对象的onRegisterSuccess方法,并传入数据phoneNumber;
if (status == 0) { registerResult = "注册成功!"; mOnRegisterSuccessListener.onRegisterSuccess(phoneNumber); }
3。哦差点忘了实例化这个对象,我们 需要重写onAttach方法,在Activity与Fragment绑定时实例化(抛出的那个异常是为了在Activity没有实现接口时给个提醒)
@Override public void onAttach(Activity activity) { super.onAttach(activity); try { mOnRegisterSuccessListener = (OnRegisterSuccessListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + "must implement OnRegisterSuccessListener!"); } }
4。控制Fragment的Activity实现这个接口,并且实现回调方法:
public class LoginActivity extends Activity implements RegFragment.OnRegisterSuccessListener
5。在实现回调方法里将A传递过来的数据用Bundle传递到FragmentB中:
@Override public void onRegisterSuccess(String phoneNumber) { LogFragment logFragment = new LogFragment(); Bundle bundle = new Bundle(); bundle.putString("phoneNumber",phoneNumber); logFragment.setArguments(bundle); getFragmentManager().beginTransaction().replace(R.id.container, logFragment).commit(); }
6。最后一步,在FragmentB中接收数据:
Bundle bundle = getArguments(); if (bundle != null){ String phoneNumber = bundle.getString("phoneNumber"); if (!TextUtils.isEmpty(phoneNumber)) { etNumber.setText(phoneNumber); } }else { LogUtils.e(TAG,"Bundle is null !"); }
7。这样就实现了2个Fragemnt间的数据通信。
学习地址:Communicating with Other Fragments
1 0
- Fragment间通信传递数据 Communicating with Other Fragments
- 11Communicating with Other Fragments(Fragment之间的通信)
- Communicating with Other Fragments
- Communicating with Other Fragments
- 《Training:Communicating with Other Fragments》
- Android — Communicating with Other Fragments
- Building a Dynamic UI with Fragments - Communicating with Other Fragments
- Building a Dynamic UI with Fragments ---- Communicating with Other Fragme (三)
- Fragment Communicating with the Activity
- unit 5 Communicating with other users
- 与HTTP服务器通信(Communicating with HTTP Servers)
- fragment间的数据传递
- Communicating with the Debugger
- Building a Dynamic UI with Fragments - Creating a Fragment
- Fragment之Fragments之间的通信(3)
- 与身份验证HTTP服务器通信(Communicating with Authenticating HTTP Servers)
- ios Communicating with HTTP Servers 与HTTP服务器通信 官方文档翻译(五)
- iOS Communicating with Authenticating HTTP Servers 与HTTP服务器通信认证 官方文档翻译(六)
- TCP协议的三次握手和四次挥手
- IOS开发Swift键盘收起,键盘挡住输入模框的处理
- angular中 ng-if 指令中的ng-model等指令失效问题
- 我的第一次出国行
- Myeclipse 错误:JVM terminated. Exit code=1
- Fragment间通信传递数据 Communicating with Other Fragments
- js内置对象
- TCP与UDP区别
- 网络编程基础(3) : IO多路复用(单线程)
- 搞清tomcat中的编解码
- leetcode--Counting Bits
- Trident WordCount代码示例
- 工作变动
- linux系统调用64位汇编与32位汇编不同及兼容