浅谈android中如何在两个Activity之间互传数据

来源:互联网 发布:ubuntu 17最快的源 编辑:程序博客网 时间:2024/06/05 23:46
简介:android在两个Activity文件中互传数据
 
 
安卓初步学习,相信知识是积累出来的,于是试着写点心得和知识点
 
 
【方法一】
 
<向twoActivity传值>
Intengt intent = new Intent(ManActivity.this,twoActivity.class);
//创建一个带“收件人地址的email”
Bundle bundle = new Bundle();//创建Email内容
bundle.putString("string_key","string_value");
intent.put("key",bundle);
startActivity(intent);
<twoActivity接收>
Intent intent = getIntent();//收取数据包
Bundle bundle = intent.getBundleExtra("key");//打开数据包
bundle.getString("string_key");// 读取内容
【方法二】
用方法一代码不仅繁琐,而且效率不够,推荐第二方法
上面我们通过bundle对象来传递信息,bundle维护了一个HashMap<String,Object>对象,因为Intent内部为我们准备好了一个bundle。
<向twoActivity传输消息>
Intent intent = new intent(MainActivity.this,twoActiviyt.class);
intent.putExtra("String_key",stringview);
startActivity(intent);
<在twoActivity中接收数据>
Intent intent = getIntent();
intent.getBooleanExtra("string_key");
 
 
这样就完成在多个页面时的数据交互啦,下面在写一点两个activity之间互相反馈打包数据的方法
 
 
 
 
 
 
 
................................................................................................................................................朱芮成

 
0 0