#Bundle类介绍

Bundle主要用于传递数据;它保存的数据,是以key-value(键值对)的形式存在的。

我们经常使用Bundle在Activity之间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。当Bundle传递的是对象或对象数组时,必须实现Serializable 或Parcelable接口。下面分别介绍Activity之间如何传递基本类型、传递对象。

相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。Bundle的内部实际上是使用了HashMap类型的变量来存放putXxx()方法放入的值:

#使用方法

使用Bundle在两个Activity中传递数据

//数据写入Intent
Intent openWelcomeActivityIntent=new Intent();
Bundle myBundelForName=new Bundle();
myBundelForName.putString("Key_Name",inName.getText().toString());
myBundelForName.putString("Key_Age",inAge.getText().toString());
openWelcomeActivityIntent.putExtras(myBundelForName);
openWelcomeActivityIntent.setClass(AndroidBundel.this, Welcome.class);
startActivity(openWelcomeActivityIntent); //从Intent 中获取数据
Bundle myBundelForGetName=this.getIntent().getExtras();
String name=myBundelForGetName.getString("Key_Name");
myTextView_showName.setText("欢迎您进入:"+name);

#与Intent对比

两个Activity之间传递数据,数据的附加有两种方式:
一种是直接 intent.putxx();
另一种是  先bundle.putxx(), 然后再调用public Intent putExtras (Bundle extras)  添加bundle.

其实两种的本质是一样的。

Intent的方法:
[java] view plaincopy
public Intent putExtra(String name, boolean value); 
public Intent putExtra(String name, byte value); 
public Intent putExtra(String name, char value); 
public Intent putExtra(String name, short value); 
public Intent putExtra(String name, int value); 
public Intent putExtra(String name, long value); 
public Intent putExtra(String name, float value); 
public Intent putExtra(String name, double value); 
public Intent putExtra(String name, String value); 
public Intent putExtra(String name, CharSequence value); 
public Intent putExtra(String name, Parcelable value); 
public Intent putExtra(String name, Parcelable[] value); 
...

intent内部定义了很多put方法,功能都是把key-value存进来。具体put函数的内部实现:
[java] view plaincopy
public Intent putExtra(String name, boolean value) { 
    if (mExtras == null) { 
        mExtras = new Bundle(); 
    } 
    mExtras.putBoolean(name, value); 
    return this; 

其中mExtras是intent内部定义的一个private Bundle变量。
可以看到,intent其实是调用了bundle相应的put函数,也就是说,intent内部还是用bundle来实现数据传递的,只是封装了一层而已。

再来说Bundle:
[java] view plaincopy
public void putBoolean(String key, boolean value); 
public void putByte(String key, byte value); 
public void putChar(String key, char value); 
 
... 
再来看用法:
只用intent:类型什么的是不需要你来操心的,你只需要putExtra就好了,内部会都存在一个bundle对象中。key-value对是一个一个被加进去的。
用intent和bundle:key-value对先被一个个的加到bundle里面,再把这个bundle put到intent中,其中用了下面这个函数:
[java] view plaincopy
public Intent putExtras(Bundle extras) { 
    if (mExtras == null) { 
        mExtras = new Bundle(); 
    } 
    mExtras.putAll(extras); 
    return this; 

可以看到,其实是把之前那个bundle中的数据批量添加到intent内部的bundle中。
取数据的时候,可以一个个的取出来(这个不赘述了),也可以把数据打包一起取出来:
[java] view plaincopy
public Bundle getExtras() { 
    return (mExtras != null) 
            ? new Bundle(mExtras) 
            : null; 
}

这个函数是把当前intent中所有的数据一起打包的(假如说你既用了bundle也用了intent本身的put函数来加数据,最后用get函数返回的是bundle+其他数据一起的)。

两者的区别是什么,如果你想对数据进行比较灵活的操作(批量操作什么的)的话就用bundle吧,当然你也可以getIntent()之后直接添加数据然后把这个intent发送出去。
还有就是,Bundle是可以对对象进行操作的,而Intent不可以。Bundle相对于Intent比较偏下层,比Intent接口更多,更灵活,但Bundle仍需要借助Intent才能在Activity之间传递。
概括一下,Intent旨在数据传递,bundle旨在存取数据,当然intent也提供一部分数据的存取,但比起bundle就显得不专业,不灵活的多

Android Bundle的更多相关文章

  1. android bundle存放数据详解

    转载自:android bundle存放数据详解 正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中 例如: 将数据存放倒Intent中传递: 将数据放到Bun ...

  2. 解决React Native unable to load script from assets index.android.bundle on windows

    React Native运行的时候,经常碰到React Native unable to load script from assets index.android.bundle on windows ...

  3. React Native: unable to load scripts from assets 'index.android.bundle' on real device

    问题:重新建了一个项目后,运行react-native run-android报: unable to load scripts from assets 'index.android.bundle' ...

  4. Unable to load script from assets 'index.android.bundle'.make sure you bundle is packaged correctly

    解决此问题 以下方法每次都需要执行命令2才能更新 1.创建assets目录 mkdir android/app/src/main/assets 2.执行命令 react-native bundle - ...

  5. 项目初始化以后出现:Unable to load script from assets 'index.android.bundle

    Mac中真机测试React Native project时出现Unable to load script from assets 'index.android.bundle' 2018年01月21日 ...

  6. react native中Unable to load script from assets 'index.android.bundle'解决方案

    刚刚朋友问我,说是创建好一个项目,运行后报错:Unable to load script from assets 'index.android.bundle',以前好好的没出现这种现象,于是我找到一个 ...

  7. Unable to load script from assets 'index.android.bundle' 出错?

    野路子太多,坑人真的!F**k 言归正传,当你运行 react native 程序的时候出现这个错误 ,如果您使用Windows,请按以下方式运行命令,或者如果出现错误“无法找到条目文件index.a ...

  8. React-Native 之 index.android.bundle

    问题: index.android.bundle  这个bug 我相信很少同学会遇到,然而就是这个问题,困扰了我跟我的同事多天, 各种方法处理:  进入 android 目录  ./gradlew c ...

  9. Android——Android Bundle详解(转)

    Android Bundle详解 1 Bundle介绍 Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的. 我们经常使用Bundle在Activity之间传递数 ...

  10. Unable to load script from assets 'index.android.bundle'.Make sure your bundle is packaged correctly or you're running a packager server

    curl -k 'http://localhost:8081/index.android.bundle?platform=android' > android/app/src/main/asse ...

随机推荐

  1. VC6.0 C++ 如何调用微软windows系统SDK 语音API

    下载3个语音API安装包 http://www.microsoft.com/en-us/download/details.aspx?id=10121 需要安装微软语音API安装包:SpeechSDK5 ...

  2. 通过url 下载文件

    1.问题简介 通过文件的url,将文件下载到本地.文件存储的位置为:tomcat服务器的文件夹(通过读取properties文件:可看:http://www.cnblogs.com/0201zcr/p ...

  3. PHP Redis

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Myredis { //redis所 ...

  4. FineReport集成到AWS系统中的方案

    本人实施了北京炎黄盈动的BPM及OA系统,主要目标是对业务流程进行控制和管理,加快Oracle JDE的业务前端录单速度和弥补JDE在流程控制方面的不足,实现BPM数据能与JDE无缝互相结合,经过3个 ...

  5. finereport与OA系统集成的完全方案

    随着社会信息化高速发展,企业信息化也得到了一定提高,而如何提高办公效率已经成为企业一项重要而紧迫的任务,传统的纸质报表等档案不仅浪费纸张.不易存档.不易调阅.不易统计,如何更有效.更快速提升办公效率和 ...

  6. [转]SQL Server 高性能写入的一些总结

    本文转自:http://www.cnblogs.com/rush/archive/2012/08/31/2666090.html 1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起 ...

  7. Booth Multiplication Algorithm [ASM-MIPS]

    A typical implementation Booth's algorithm can be implemented by repeatedly adding (with ordinary un ...

  8. 02 Hibernate错题分析

    解析:使用final修饰的成员变量是常量 解析:不存在StateMoreSession的对象 解析:一个PreparedStatement 可以执行多次executQuery方法 解析:A   使用H ...

  9. createDocumentFragment() 创建文档碎片节点

    var aqiData = [ ["北京", 90], ["上海", 50], ["福州", 10], ["广州", 5 ...

  10. [No00004A]为什么你看了很多书,却依然没有洞见

    摘要: 前几天有人在知乎上问:今天就回答下很多人问了很久的这个问题,并且解释一下如何构建系统化的知识体系.我想很多人看到这个问题,期待的答案是一个书单,可是我要告诉你这并没有什么卵用.我想大部分人都经 ...