首先activity之间的数据传递就是

用intent和intent+bundle

intent

传递

Intent i= new Intent(MainActivity.this,TheAty.class);
i.putExtra("date","Hello SWWWWWW");
startActivity(i);

接受数据

 Intent i =getIntent();

 tv=(TextView) findViewById(R.id.tv);

      //通过“date”关键字进行添加tv.setText(i.getStringExtra("date"));

intent+bundle

传递数据

   Intent i= new Intent(MainActivity.this,TheAty.class);
Bundle b=new Bundle();
b.putString("name","SWWWWW");
b.putInt("age",21);
b.putString("depart","KuaiJi");
i.putExtras(b);
startActivity(i);

接受数据

 Intent i =getIntent();
Bundle data=i.getExtras();
tv=(TextView) findViewById(R.id.tv);
tv.setText(String.format("name=%s,age=%d,depart=%s",data.getString("name"),data.getInt("age"),data.getString("depart")));

下面就是Activity与fragment之间的传递数据

activity传给fragment

第一种

MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("DATA",values);//这里的values就是我们要传的值
myFragment.setArguments(bundle);

第二种

//宿主activity中的getTitles()方法
public String getTitles(){
return "hello";
} //Fragment中的onAttach方法
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
titles = ((MainActivity) activity).getTitles();
}
//通过强转成宿主activity,就可以获取到传递过来的数据

3 这个回调方法,在很多api中经常使用,我就看了一天api我就知道这种回调经常使用而且非常有用

Fragment向activity中传值

1.在Fragment中写一个回调接口

2.在activity中实现这个回调接口

3,在Fragment中onAttach 方法中得到activity中实现好的 实例化接口对象

4,用接口的对象  进行传值

activity

package com.qianfeng.fragmenttoactivity;  

import com.qianfeng.fragmenttoactivity.Fragmen1.CallBackValue;  

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.widget.TextView; @SuppressLint("NewApi")
public class MainActivity extends Activity implements CallBackValue{ private TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.tv1); FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.contents, new Fragmen1());
transaction.commit(); }
//要获取的值 就是这个参数的值
@Override
public void SendMessageValue(String strValue) {
// TODO Auto-generated method stub
tv1.setText(strValue);
} }

Frag

@SuppressLint("NewApi")
public class Fragmen1 extends Fragment{
private Button btn1;
private EditText et1;
CallBackValue callBackValue; /**
* fragment与activity产生关联是 回调这个方法
*/
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
//当前fragment从activity重写了回调接口 得到接口的实例化对象
callBackValue =(CallBackValue) getActivity();
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_layout1, container, false);
btn1 = (Button) view.findViewById(R.id.btn1);
et1 = (EditText) view.findViewById(R.id.et1);
btn1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String strValue = et1.getText().toString().trim();
callBackValue.SendMessageValue(strValue); }
}); return view;
}
//定义一个回调接口
public interface CallBackValue{
public void SendMessageValue(String strValue);
}
}

ment

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

  1. 两个Fragment之间如何传递数据

    FragmentA启动FragmentB,做一些选择操作后,返回FragmentA,需要把FragmentB里面选择的数据传回来.有什么办法? Fragment之间不能直接通信,必须通过Activit ...

  2. 用Broadcast广播在activity之间、fragment之间、activity和fragment之间相互传数据

    例如:A界面要收到B界面的更变信息 一.A界面注册广播 private static final String INTENT_BROADCAST = "android.intent.acti ...

  3. 在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程。

    在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程. 答案:可以通过Intent对象.静态变量.剪切板和全局对象进行数据传递,具体的数据传递方法如下. 1. ...

  4. Android消息机制之实现两个不同线程之间相互传递数据相互调用

    目的:实现两个不同线程之间相互传递数据相互调用方法. 线程一中定义mainHandler 并定义一个方法mainDecode 线程二中定义twoHandler 并定义一个方法twoEncode 实现当 ...

  5. Activity和Fragment之间解耦

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

  6. activity与fragment之间传递数据

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

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

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

  8. Activity与Fragment之间的通信

    由于Fragment的生命周期完全依赖宿主Activity,所以当我们在使用Fragment时难免出现Activity和Fragment间的传值通信操作. 1.Activity向Fragment,通过 ...

  9. Android Activity间跳转与传递数据

    1 概述 Activity之间的跳转主要使用 startActivity(Intent intent); startActivityForResult(Intent intent,int reques ...

随机推荐

  1. 05_Spring AOP原理

    理解AOP相关概念 Target(目标对象):代理的目标对象 Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. ...

  2. 03_Hibernate关系映射

    关系映射? Hibernate的主要目的就是JAVA程序员可以随心所欲的使用对象编程思维来操作数据库.一些数据库表的关系我们应该可以通过hibernate实现,比如数据库中用到的主外键关系,还有一些与 ...

  3. HLog工作原理

  4. Luogu P3496 [POI2010]GIL-Guilds(贪心+搜索)

    P3496 [POI2010]GIL-Guilds 题意 给一张无向图,要求你用黑(\(K\))白(\(S\))灰(\(N\))给点染色,且满足对于任意一个黑点,至少有一个白点和他相邻:对于任意一个白 ...

  5. Windows API 第16篇 GetLogicalDrivers 获取驱动器位掩码

    函数原型:DWORD GetLogicalDrives(VOID);The GetLogicalDrives function retrieves a bitmask representing the ...

  6. Android 开发 Camera2开发_3_处理预览和拍照偏暗问题

    通过调整曝光解决 参考:https://stackoverflow.com/questions/28429071/camera-preview-is-too-dark-in-low-light-and ...

  7. spfa模版

    #include<bits/stdc++.h> using namespace std; int n,m;//点边 int beginn; ],v[],w[]; ],nextt[]; ]; ...

  8. LUOGU P2962 [USACO09NOV]灯Lights

    题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...

  9. 机器学习实战之Apriori

    机器学习实战之Apriori 1. 关联分析 1.1 定义        关联分析是一种在大规模数据上寻找物品间隐含关系的一种任务.这种关系有2种形式:频繁项集和关联规则. (1) 频繁项集(freq ...

  10. VS C++/ClI调用C++ 外部Dll无法查看变量值

    C#项目调用C++/ClI项目,C++/ClI项目又引用了外部C++ dll时 C++/CLI代码中在调试时无法查看native 变量的值 解决方法:C#项目右键属性-->Debug--> ...