Bundle类

来源:互联网 发布:淘宝云客服考试题库 编辑:程序博客网 时间:2024/06/05 09:46

Bundle在Activity之间传递数据,传递的数据可以是booleanbyteintlongfloatdoublestring等基本类型或它们对应的数组,也可以是对象或对象数组。当Bundle传递的是对象或对象数组时,必须实现Serializable 或Parcelable接口。由此看出bundle比intent更强大

Bundle类是一个key-value对,“A mapping from String values to various Parcelable types.

类继承关系:

Java.lang.Object
     Android.os.Bundle

Bundle类是一个final类:
public final class
Bundle
extends Objectimplements Parcelable Cloneable

两个activity之间的通讯可以通过bundle类来实现,做法就是:

(1)新建一个bundle类

[java] view plain copy
  1. Bundle mBundle = new Bundle();   
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
[java] view plain copy
  1. mBundle.putString("Data""data from TestBundle");  

(3)新建一个intent对象,并将该bundle加入这个intent对象
[cpp] view plain copy
  1. Intent intent = new Intent();    
  2. intent.setClass(TestBundle.this, Target.class);    
  3. intent.putExtras(mBundle); 
0 0
原创粉丝点击