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 ...
随机推荐
- 详细介绍android rom移植知识普及
详细介绍android rom移植知识普及 最近接到很多兄弟们的求助,也回答过无数个和下面这个问题类似的问题: 如何编译android 原生代码得到一个rom,然后跑到某某手机上. 鉴于很多兄弟对这块 ...
- poj 3181 Dollar Dayz (整数划分问题---递归+DP)
题目:http://poj.org/problem?id=3181 思路:将整数N划分为一系列正整数之和,最大不超过K.称为整数N的K划分. 递归:直接看代码: 动态规划:dp[i][j]:=将整数i ...
- android GestureDetector 手势的判断
import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Ges ...
- 简析MFC中CString用作C字符串
MFC中CString是一个方便的字符串操作的类, 然而很多函数需要传递字符指针, 这就需要进行CString和普通字符串的转换. 1.CString用作C字符串常量. 直接使用强制类型转换即可, ...
- dsadm-dsconf数据导入导出
cd instance-path/ds6/bin #注意黄色参数修改为跟实际环境一致 -c,--accept-cert Does not ask for confirmation before a ...
- HttpClient get返回String类型 JAVA
public static String httpGet(String url) { // get请求返回结果 String strResult = ""; try { Defau ...
- No suitable authentication method found to complete authentication (publickey,keyboard-interactive).
string command = Command.Text; StringBuilder result = new StringBuilder(); try { var connectionInfo ...
- 在div+css中用到的js代码注意return
今天做了一个项目,美工做好后放在了form中(没有加runat=server),由于用到了服务器控件,所以这里要加,否则报错,关键一段div代码是: <form id="form_re ...
- Hashtable键值集合
//Hashtable键值集合 键必须是维一的 类似于索引 Hashtable ht = new Hashtable(); ht.Add(, "中国"); ht.Add(, ); ...
- java基础day7
1/匿名类对象:创建类的对象是匿名的. 比如说new Circle():就是一个匿名类对象. 匿名类对象只能使用一次. 2/形参:声明方法时,方法小括号内的参数 实参:调用方法是,实际传入的参数的值 ...