总结:无论是activity给fragment传递数据,还是fragment给activity传递数据,都把activity和fragment都当做一个普通的对象,调用它的方法,传递参数。

1.Fragment中通过getActivity()然后进行强制转化,调用Activity中的公有方法

((XXXXActivity)getActivity()).fun();

2.Activity在切换Fragment的时候,通过setArguments向Fragment传递参数,Fragment通过getArguments();获得从activity中传递过来的值

实例界面:(此Demo演示了activity给fragment传递数据和fragment给activity传递数据)

代码如下:

activity_main.xml

<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" >

<LinearLayout
android:layout_width="120dip"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity界面"
android:textSize="18sp" />

<Button
android:layout_width="120dip"
android:layout_height="wrap_content"
android:onClick="open01"
android:text="fragment01" />

<EditText
android:id="@+id/et_data"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:hint="请输入数据" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submit"
android:text="提交" />
</LinearLayout>

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#ff000f" />

<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>

</LinearLayout>

----------------------------------------------------------------------------------------------------------------

f01.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Frgment01界面"
android:textSize="18sp" />

<TextView
android:id="@+id/tv_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp" />

<EditText
android:id="@+id/et_info"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:hint="请输入信息" />

<Button
android:id="@+id/btn_info"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="传递" />

</LinearLayout>

------------------------------------------------------------------------------------------------------

MainActivity.java

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends FragmentActivity {

private Fragment01 f01;
private FragmentManager fm;
private EditText et_data;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et_data = (EditText) findViewById(R.id.et_data);

f01 = new Fragment01();

// 得到fragment的管理器,可以得到fragment的事务管理器
fm = this.getSupportFragmentManager();

open01(null);
}

public void open01(View view) {

// Fragment01 f01= new Fragment01();

// 得到fragment的事务管理器,可以填充fragment的界面
// 事务可以保证加载的多个fragment要么同时显示成功要么同时显示失败
FragmentTransaction ft = fm.beginTransaction();
// 把f01显示在framelayout界面中
ft.replace(R.id.fl_container, f01);
// 提交事务
ft.commit();
}

// Activity传递数据给fragment
public void submit(View v) {
// 传递数据给fragment01界面
String data = et_data.getText().toString().trim();
// 把data传给fragment01
// 把fragment01作为一个普通的对象,调用它的方法传递参数
// Fragment01 f01 = new Fragment01();
// activity中设置数据,对应fragment有获取数据的方法。
f01.setData(data);
}

// 获取fragment传递过来的数据。
public void setInfo(String info) {
et_data.setText(info);

}

}

-----------------------------------------------------------------------------------------------

fragment01.xml

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Fragment01 extends Fragment {

private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 加载布局文件
// getActivity() fragment所运行在的activity
// resource fragment的布局文件资源ID
// root 父级控件,通常使用null

view = View.inflate(getActivity(), R.layout.f01, null);

// 从view中找到按钮
Button btn_info = (Button) view.findViewById(R.id.btn_info);
btn_info.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

EditText et_info = (EditText) view.findViewById(R.id.et_info);
String info = et_info.getText().toString().trim();
// 把MainActivity作为一个普通的对象,调用它的方法传递参数
FragmentActivity activity = getActivity();
// 判断activity是否是MainActivity类型的
if (activity instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) activity;
// 调用activity的方法传递参数
mainActivity.setInfo(info);
}

}
});
return view;
}

// fragment传递数据给activity
public void setData(String data) {
TextView tv_data = (TextView) view.findViewById(R.id.tv_data);
tv_data.setText(data);

}

}

activity与fragment之间传递数据的更多相关文章

  1. 两个Fragment之间传递数据

    1.第一个Fragment BlankFragment blankFragment = new BlankFragment();Bundle bundle = new Bundle();bundle. ...

  2. Android 笔记-Fragment 与 Activity之间传递数据

    Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...

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

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

  4. Activity之间传递数据的方式及常见问题总结

    Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...

  5. Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口

    package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...

  6. 28、activity之间传递数据&批量传递数据

    import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...

  7. 【Android 复习】 : Activity之间传递数据的几种方式

    在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...

  8. Android基础知识04—Activity活动之间传递数据

    ------活动之间传递数据------ 向下一个活动传递数据: Intent中提供了一系列的putExtra()方法,可以把数据暂存到Intent中,启动另一个活动的时候就可以取出来. 代码: (存 ...

  9. Android Fragment之间传递List数据

    要说的是在两个Fragment之间传递List数据,比如有个List<User>,以及传递字符串数据,比如testId,该如何从FragmentA传递到FragmentB呢? 下面这个例子 ...

随机推荐

  1. 泛型T的类型获取

    T.getClass()或者T.class都是非法的,因为T是泛型变量. 由于一个类的类型是什么是在编译期处理的,故不能在运行时直接在Base里得到T的实际类型. /** * 可以在service层直 ...

  2. 远程CDN加速不可用,加载本地库

    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery ...

  3. Guava学习笔记(一)概览

    Guava是谷歌开源的一套Java开发类库,以简洁的编程风格著称,提供了很多实用的工具类, 在之前的工作中应用过Collections API和Guava提供的Cache,不过对Guava没有一个系统 ...

  4. 动态创建 Lambda 表达式

    首先我们看一个简单 Lambda 表达式的构成. i => i > 5 在这个表达式中,"i" 被称为 Parameter,"i > 5" 是 ...

  5. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  6. OpenGL帧缓存对象(FBO:Frame Buffer Object)(转载)

    原文地址http://www.songho.ca/opengl/gl_fbo.html 但有改动. OpenGL Frame BufferObject(FBO) Overview: 在OpenGL渲染 ...

  7. linux进程

    E: 进程------->进程控制块PCB   结构体 进程控制块中,存放的是指针数组------>是已经打开的文件的结构体的指针 文件描述符实际上就是指针数组的索引 e1: ps命令(进 ...

  8. 工作记录 java

    1:tomcat有两个这个文件,是缓存文件和临时文件.可能会缓存部分的jsp文件,所以如果有时候文件重新替换不起效果的话,可以先把这两个文件夹下面的文件清空

  9. 最大流-最小割 MAXFLOW-MINCUT ISAP

    简单的叙述就不必了. 对于一个图,我们要找最大流,对于基于增广路径的算法,首先必须要建立反向边. 反向边的正确性: 我努力查找了许多资料,都没有找到理论上关于反向边正确性的证明. 但事实上,我们不难理 ...

  10. 从零开始山寨Caffe·陆:IO系统(一)

    你说你学过操作系统这门课?写个无Bug的生产者和消费者模型试试! ——你真的学好了操作系统这门课嘛? 在第壹章,展示过这样图: 其中,左半部分构成了新版Caffe最恼人.最庞大的IO系统. 也是历来最 ...