Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)
Fragment与Activity之间的数据交换,通常含有3:
一、Fragment从Activity获取数据(仅本文介绍了一个第一);
两、Activity从Fragment获取数据;
三、Fragment之间的数据访问。
实现效果图:
从Activity传递数据到两个Fragment中,Fragment获取数据后,展示出来。
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjQ0MDIwNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="500" height="300" alt="">
源码:
布局文件:
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="horizontal"> <LinearLayout
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2"
android:background="#CCCCCC">
</LinearLayout> <LinearLayout
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
</LinearLayout> </LinearLayout>
MyFragment1的布局文件f1:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
MyFragment2的布局文件f2:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
代码文件:
MainActivity:
package com.fragmentdemo5_commute; import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle; /**
* 一、Fragment从Activity获取数据。 */
public class MainActivity extends Activity {
private FragmentManager manager;
private FragmentTransaction transaction; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); manager = getFragmentManager();
transaction = manager.beginTransaction(); MyFragment1 fragment1 = new MyFragment1();
Bundle bundle1 = new Bundle();
bundle1.putString("id", "Activity发送给MyFragment1的数据");
fragment1.setArguments(bundle1);
transaction.replace(R.id.left, fragment1, "left"); MyFragment2 fragment2 = new MyFragment2();
Bundle bundle2 = new Bundle();
bundle2.putString("id", "Activity发送给MyFragment2的数据");
fragment2.setArguments(bundle2);
transaction.replace(R.id.right, fragment2, "right"); transaction.commit();
} }
MyFragment1:
package com.fragmentdemo5_commute; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class MyFragment1 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f1, null); TextView textView = (TextView) view.findViewById(R.id.textView);
Bundle bundle1 = getArguments();
textView.setText(bundle1.getString("id"));
return view;
} }
MyFragment2:
package com.fragmentdemo5_commute; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class MyFragment2 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f2, null); TextView textView = (TextView) view.findViewById(R.id.textView);
Bundle bundle2 = getArguments();
textView.setText(bundle2.getString("id"));
return view;
} }
源码下载:
Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)的更多相关文章
- 微信小程序 存储数据到本地以及本地获取数据
1.wx存储数据到本地以及本地获取数据 存到本地就是存到你的手机 wx.setStorageSync与wx.setStorage 1.1 wx.setStorageSync(string key, a ...
- android Activity之间数据传递 Parcelable和Serializable接口的使用
Activity之间传数据时,为了避免麻烦,往往会将一些值封装成对象,然后将整个对象传递过去.传对象的时候有两种情况,一种是实现Parcelable接口,一种是实现Serializable接口.0.解 ...
- 【Android基础】利用Intent在Activity之间传递数据
前言: 上一篇文章给大家聊了Intent的用法,如何用Intent启动Activity和隐式Intent,这一篇文章给大家聊聊如何利用Intent在Activity之间进行沟通. 从一个Activ ...
- Android之Activity之间跳转
本人自学Android,想到什么就写点什么.主要是怕忘了,哈哈~请观者不要建议~ 今天写点Android窗口之间的跳转以及自己理解: 1.Android中窗口之间的跳转,就是Activity之间的跳转 ...
- android入门:activity之间跳转,并且回传参数
介绍: 两个activity进行跳转,在跳转过程中,将message由MainActivity传递到secondActivity,并且当secondActivity退回至MainAct ...
- android脚步---不同activity之间参数传递
现在有两个activity,一个是mainactivity,一个是detectactivity 后者需要调用前者的一个参数,这里用到了intent getextras(); putextras(); ...
- java并发编程JUC第十一篇:如何在线程之间进行对等数据交换
java.util.concurrent.Exchanger可以用来进行数据交换,或者被称为"数据交换器".两个线程可以使用Exchanger交换数据,下图用来说明Exchange ...
- 如何实现两个Activity 之间如何通讯
<转> 今天主要学习了Activity 组件,在这里作一下总结 1,学习如何创建Activity 创建 Activity 要点: (1) 一个Activity就是一个类,并且这个类要继承A ...
- [ Android 五种数据存储方式之一 ] —— SharedPreferences存储数据
SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. 主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceS ...
- DataStage 九、数据交换到MySQL以及乱码问题
DataStage序列文章 DataStage 一.安装 DataStage 二.InfoSphere Information Server进程的启动和停止 DataStage 三.配置ODBC Da ...
随机推荐
- 基本介绍LINUX远程PC软件:PUTTY、SecureCRT、X-Manager
***********************************************声明************************************************ 原创 ...
- 综合第一篇文章(带钩Quora)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDc4MzAyNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Java设计模式菜鸟系列(两)建模与观察者模式的实现
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39755577 观察者(Observer)模式定义:在对象之间定义了一对多的依赖关系,这样一 ...
- Swift1_关闭
// main.swift // swift1_关闭 // Created by beyond on 15/6/12. // Copyright (c) 2015年 beyond.com All ri ...
- SQL Server 2008 R2 跟踪标志
原文:SQL Server 2008 R2 跟踪标志 跟踪标志用于临时设置特定服务器的特征或关闭特定行为.例如,如果启动 SQL Server 的一个实例时设置了跟踪标志 3205,将禁用磁带机的硬件 ...
- 在 Ubuntu 12.04 上安装 GitLab7.x
安装环境: 操作系统: Ubuntu 12.4 LTS 英文 数据库: postgresql webserver: nginx 能够说到7.x的时候,GitLab的文档已经相当完好 ...
- BZOJ 2435 NOI2011 道路建设 BFS/DFS
标题效果:给定一个树(直接将树.不要贪图生成树图!).寻找每条边权值*分差的两侧之间 BFS水必须是能 竟DFS能够住...系统堆栈可能有些不够,我们可以使用内联汇编手册中大型系统堆栈 详见代码 这个 ...
- 百度云BAE3.0 的ssh构造(本机ssh项目迁移到BAE3.0)
依据百度云的java部署文档进行部署 http://developer.baidu.com/wiki/index.php?title=docs/cplat/bae/java 做例如以下改动,然后把项目 ...
- jquery php 百度搜索框智能提示效果
这个程序是利用php+ajax+jquery 实现的一个仿baidu智能提示的效果,有须要的朋友能够下载測试哦. 代码例如以下 index.html文件,保保存成index.htm <!DOCT ...
- python 2.x 与3.x的区别
下载了点python的电子书,有基于3.x的有基于2.x的让我不知道看哪些好,BD一下差别着实很大有木有?print语句变成了函数有木有?之后都得加括号了有木有? 别人整理的成果,我就无耻地搬来借用啦 ...