#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. html5全局属性

    全局属性:对于任何一个标签都是可以使用的属性. 一.data-* 在html5之前需要在html标签上添加自定义属性来存储和操作数据,可能是会写<form role="xxx" ...

  2. java 知识点随记

    JAVA 读取配置文件: Properties props= new Properties();//文件在src目录下,编译会被加载到classpath下. Props.load(Test.class ...

  3. [Basic Information Theory] Writen Notes

  4. 【转载】SweetAlert2 使用

    SweetAlert2是一款功能强大的纯Js模态消息对话框插件.SweetAlert2用于替代浏览器默认的弹出对话框,它提供各种参数和方法,支持嵌入图片,背景,HTML标签等,并提供5种内置的情景类, ...

  5. MVC编写的新闻页面

    1.新闻发布系统 2.架构确立 3.数据表确立 4.分层 entity dao BaseDao sqlserver jar包 接口层(NewsDetailDAO) impl (NewsDetailDA ...

  6. 那些强悍的PHP一句话后门

    强悍的PHP一句话后门这类后门让网站.服务器管理员很是头疼,经常要换着方法进行各种检测,而很多新出现的编写技术,用普通的检测方法是没法发现并处理的.今天我们细数一些有意思的PHP一句话木马.利用404 ...

  7. 逗号分隔的字符串转换为行数据(collection)(续)

    逗号分隔的字符串转行数据的存储过程一个: CREATE OR REPLACE FUNCTION SP_YX_SPLIT ( p_list CLOB, p_sep VARCHAR2 := ',' ) R ...

  8. Hibernate总结4之HQL

    一,HQL特点 与SQL相似,SQL中的语法基本上都可以直接使用. SQL查询的是表和表中的列:HQL查询的是对象与对象中的属性. HQL的关键字不区分大小写,类名与属性名是区分大小写的. SELEC ...

  9. 交叉验证 Cross validation

    来源:CSDN: boat_lee 简单交叉验证 hold-out cross validation 从全部训练数据S中随机选择s个样例作为训练集training set,剩余的作为测试集testin ...

  10. Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...