由于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之间的通信的更多相关文章

  1. Android 数据传递(二)Activity与fragment之间的通信

    在网上找到了一篇总结的非常好的文章,我这里就贴出他的博文地址.自己就不再写这个方面的总结了. Activity与Fragment通信(99%)完美解决方案

  2. Fragment的生命周期&同一Activity下不同Fragment之间的通信

    Android开发:碎片Fragment完全解析(2) Fragment的生命周期 和Activity一样,Fragment也有自己的生命周期,理解Fragment的生命周期非常重要,我们通过代码的方 ...

  3. Fragment之间的通信(四)

    自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...

  4. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  5. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...

  6. [转][译][Android基础]Android Fragment之间的通信

    2014-2-14 本篇文章翻译自Android官方的培训教程,我也是初学者,觉得官方的Training才是最好的学习材料,所以边学边翻译,有翻译不好的地方,请大家指正. 如果我们在开发过程中为了重用 ...

  7. Activity和Fragment之间解耦

    看鸿洋博客:http://blog.csdn.net/lmj623565791/article/details/42628537,整理下一些关键点 public class ContentFragme ...

  8. activity与fragment之间的传递数据

    首先activity之间的数据传递就是 用intent和intent+bundle intent 传递 Intent i= new Intent(MainActivity.this,TheAty.cl ...

  9. Fragment之间的通信

    在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...

随机推荐

  1. Android软键盘强制弹出,隐藏输入法.

    本文实例讲述了Android实现弹出键盘代码,是一个非常实用的功能.代码非常简洁.分享给大家供大家参考. 具体功能代码如下: ? 1 2 3 4 5 6 7 8 Timer timer = new T ...

  2. AIX-du

    du命令显示用于文件的块的数量.如果指定的File参数实际上是一个目录,就要报告该目录内的所有文件.如果没有提供 File参数,du命令使用当前目录内的文件.如果File参数是一个目录,那么报告的块的 ...

  3. jquery之鼠标失去焦点事件

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  4. 关于textbox.attributes["value"]的问题

    在“修改”时,出现这个问题,后台点击修改时,应该是文本框出现一些初始值 BLL.manager bll = new BLL.manager(); Model.manager model = bll.G ...

  5. 查看Linux相关信息

    1."uname -a ",可显示电脑以及操作系统的相关信息. 2."cat /proc/version",说明正在运行的内核版本. 3."cat / ...

  6. sqlite--代码操作

    1.创建数据库 NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainM ...

  7. OC基础 代理和协议

    OC基础 代理和协议 1.协议 (1)oc语言中得协议:一组方法列表,不需要我们自己实现,由遵守协议的类来实现协议所定制的方法. (2)协议的使用步骤:制定协议-->遵守协议-->实现协议 ...

  8. 提升PHP速度的53个建议

      1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row[’id’] 的速度是$row[id]的7倍. 3.echo 比 print 快,并且使用e ...

  9. MySQL 聚簇索引

    聚簇索引并不是一种单独的索引类型,而是一种数据存储方式.具体的细节依赖于其实现方式,但innoddb 的聚簇索引实际上在同一个结构中保存了B-Tree索引和数据行. 当表有聚簇索引时,它的数据实际上存 ...

  10. java学习笔记 (8) —— Struts2 实现上传

    1.新建upload.jsp <%@ page language="java" import="java.util.*" pageEncoding=&qu ...