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 ...
随机推荐
- 定时改变App主题的方案
1.将接口返回的图片缓存到本地,由于写data到本地是耗时操作,为了不阻塞主线程,可开启子线程来做此操作 dispatch_queue_t queue = dispatch_queue_create( ...
- [转] iOS性能优化技巧
(转自:hhttp://www.raywenderlich.com/31166/25-ios-app-performance-tips-tricks#arc, http://blog.ibireme. ...
- javascript获取标签样式(获取背景为例)
function getStyle(el){ if(window.getComputedStyle){ return window.getComputedStyle(el,null); } retur ...
- Asp.Net WebApi+Microsoft.AspNet.WebApi.Core 启用CORS跨域访问
WebApi中启用CORS跨域访问 1.安装 Nugget包Microsoft.AspNet.WebApi.Cors This package contains the components to e ...
- MYSQL显示数据库内每个表拥有的触发器
一 所有数据库->所有触发器: SELECT * FROM information_schema.triggers; 二 当前数据库->当前所有触发器(假设当前数据库为gmvcs_ba ...
- 通俗理解angularjs中的$apply,$digest,$watch
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...
- node.js 上传文件
在工作中碰到了这样的需求,需要用nodejs 来上传文件,之前也只是知道怎么通过浏览器来上传文件, 用nodejs的话, 相当于模拟浏览器的行为. google 了一番之后, 明白了浏览器无非就 ...
- C/C++中new关键字是否加括号的区别
代码: #include <iostream> using namespace std; class A{ public: int a; }; int main(){ A *a1 = ne ...
- 你好,C++(20).4.2.2 表达并列条件选择的switch语句:如果……如果……如果……
4.2.2 表达并列条件选择的switch语句:如果……如果……如果…… 在现实世界中,还有这样一类特殊的条件选择: 如果明天是晴天,我就穿T恤: 如果明天是阴天,我就穿衬衣: 如果明天是雨天,我就 ...
- C# 解析json Newtonsoft.Json
Newtonsoft.Json.dll public class ErrorInfo { public error_response error_response { get; set; } } pu ...