当前位置: 代码迷 >> 综合 >> Bundle类
  详细解决方案

Bundle类

热度:70   发布时间:2024-01-16 10:19:38.0

Bundle在Activity之间传递数据,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。当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); 
  相关解决方案