Activity与Fragment之间的通信
由于Fragment的生命周期完全依赖宿主Activity,所以当我们在使用Fragment时难免出现Activity和Fragment间的传值通信操作。
1、Activity向Fragment,通过声明的Fragment对象的setArguments(bundle)方法来实现Activity到Fragment的传递
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_communication); FragmentManager fragmentMgr = getFragmentManager();
FragmentTransaction transaction = fragmentMgr.beginTransaction(); Fragment fragment = new CommunicationFragment();
Bundle bundle = new Bundle();
bundle.putCharSequence(MSG_KEY , MESSAGE);
fragment.setArguments(bundle); transaction.replace(R.id.ll_communication, fragment);
transaction.commit();
}
Fragment中获取Activity传递的数据,通过Bundle bundle = this.getArguments()方法获取数据
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_communication, container, false); Bundle bundle = this.getArguments();
msg = bundle.getCharSequence(CommunicationActivity.MSG_KEY); initView(); Toast.makeText(getActivity(), "接收到了来自Fragment的消息:" + msg, Toast.LENGTH_SHORT).show();
return view;
}
2、Fragment到Activity
在Fragment需要声明一个接口,定义回调方法,如CommunicationFragment中定义了OnFragmentInteractionListener内部接口,并声明mInteractionListener这个成员变量,当调用onAttach方法时将activity转换为OnFragmentInteractionListener,并调用它的方法传递数据。而在宿主Activity中需要实现OnFragmentInteractionListener接口,并实现方法public void onFragmentInteraction(CharSequence c),在方法中接收数据并处理
CommunicationFragment.java
package com.baixd.app.framework.fragment; import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast; import com.baixd.app.framework.R; public class CommunicationFragment extends Fragment { private View view; private TextView mTextView; private CharSequence msg; private static final CharSequence RESP_MSG = "成功接收消息!"; private OnFragmentInteractionListener mInteractionListener; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_communication, container, false); Bundle bundle = this.getArguments();
msg = bundle.getCharSequence(CommunicationActivity.MSG_KEY); initView(); Toast.makeText(getActivity(), "接收到了来自Fragment的消息:" + msg, Toast.LENGTH_SHORT).show();
return view;
} @Override
public void onAttach(Activity activity) {
mInteractionListener = (OnFragmentInteractionListener) activity;
mInteractionListener.onFragmentInteraction(RESP_MSG);
super.onAttach(activity);
} private void initView() {
mTextView = (TextView) view.findViewById(R.id.tv_communiation_text);
mTextView.setText(msg);
} public interface OnFragmentInteractionListener {
public void onFragmentInteraction(CharSequence c);
} }
宿主Activity:CommunicationActivity.java
package com.baixd.app.framework.fragment; import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast; import com.baixd.app.framework.R; public class CommunicationActivity extends ActionBarActivity implements CommunicationFragment.OnFragmentInteractionListener{ public static final String MSG_KEY = "msg"; public static final CharSequence MESSAGE = "向Fragment传递消息"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_communication); FragmentManager fragmentMgr = getFragmentManager();
FragmentTransaction transaction = fragmentMgr.beginTransaction(); Fragment fragment = new CommunicationFragment(); transaction.replace(R.id.ll_communication, fragment);
transaction.commit();
} @Override
public void onFragmentInteraction(CharSequence msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
Activity与Fragment之间的通信的更多相关文章
- Android 数据传递(二)Activity与fragment之间的通信
在网上找到了一篇总结的非常好的文章,我这里就贴出他的博文地址.自己就不再写这个方面的总结了. Activity与Fragment通信(99%)完美解决方案
- Fragment的生命周期&同一Activity下不同Fragment之间的通信
Android开发:碎片Fragment完全解析(2) Fragment的生命周期 和Activity一样,Fragment也有自己的生命周期,理解Fragment的生命周期非常重要,我们通过代码的方 ...
- Fragment之间的通信(四)
自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...
- [转][译][Android基础]Android Fragment之间的通信
2014-2-14 本篇文章翻译自Android官方的培训教程,我也是初学者,觉得官方的Training才是最好的学习材料,所以边学边翻译,有翻译不好的地方,请大家指正. 如果我们在开发过程中为了重用 ...
- Activity和Fragment之间解耦
看鸿洋博客:http://blog.csdn.net/lmj623565791/article/details/42628537,整理下一些关键点 public class ContentFragme ...
- activity与fragment之间的传递数据
首先activity之间的数据传递就是 用intent和intent+bundle intent 传递 Intent i= new Intent(MainActivity.this,TheAty.cl ...
- Fragment之间的通信
在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...
随机推荐
- C++编程规范之19:总是初始化变量
摘要: 一切从白纸开始,未初始化的变量是C和C++程序中错误的常见来源.养成在使用内存之前先清除的习惯,可以避免这种错误,在定义变量的时候就将其初始化. 按照C和C++相同的低层高效率传统,通常并不要 ...
- [原创]# 玩转nginx系列
首先先上如何彻底删除nginx 看到这个标题的小伙伴都惊呆了,还不知道怎么搞,却叫我怎么卸载.为什么我要这样,其实,Reset也是一种解决问题的方式嘛. 首先执行下卸载命令 sudo apt-get ...
- ionic学习教程地址梳理
Ionic是一个新的.可以使用HTML5构建混合移动应用的用户界面框架,它自称为是"本地与HTML5的结合".该框架提供了很多基本的移动用户界面范例,例如像列表(lists).标签 ...
- 程序猿的道路~~(How to be a programmer?)
程序猿的道路其实很简单,主要就是三条: Learn (学习), Practice(练习), Summary(总结) 推荐给新手程序猿两篇文章: 给程序员新手的一些建议 程序员技术练级攻略 当然了,整个 ...
- H面试程序(10): 字符串包含问题
题目描述:判断第二个字符串中的元素是否都能在第一个字符串中找到: 注意:和字符串的字串的问题有所区别,如第一个字符串为 abcdefg,第二个字符串为 aaabc,第二个字串还是包含于第一个字符串 ...
- [Polymer] Custom Elements: Styling
Code: <dom-module id="business-card"> <template> <div class="card" ...
- Mysqldb连接Mysql数据库(转)
python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库 ...
- iPhone手机的屏幕尺寸、分辨率及适配
1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...
- 使用Alcatraz为Xcode安装XActivatePowerMode插件, 从此敲代码逼格大大滴~
Alcatraz 是一款 Xcode的插件管理工具,可以用来管理XCode的 插件.模版以及颜色配置的工具. 关于Alcatraz的安装,这里有一篇不错的博文,请参考安装:http://www.cnb ...
- C++中的struct与class继承方式
代码: #include <iostream> #include <cstdio> using namespace std; //class A{ struct A{ publ ...