Bundle类

来源:互联网 发布:wifi热点软件 编辑:程序博客网 时间:2024/05/16 02:56

Bundle

Bundle类是一个key-value对,两个activity之间的通讯可以通过bundle类来实现。

Bundle传递数据

1、实现步骤:

a、在一个类中:

Bundle mBundle = new Bundle();//新建一个bundle类  

mBundle.putString("key", "value");  

//bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value) 

Intent intent = new Intent();//新建一个intent对象   

intent.setClass(activity_1.this, activity_2.class);    

intent.putExtras(mBundle); //将该bundle加入这个intent对象 

startActivity(intent);

b、在另一个类中:

    Bundle bundle = getIntent().getExtras();    //得到传过来的bundle

String data = bundle.getString("Data");//读出数据  

 

2、思路:

(key,vlaue)--->Bundle--->intent-->Bundle-->通过key找到vlaue

 

Bundle传递对象

一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口

方式一:Bundle.putSerializable(Key,Object);

 

Bundle data = new Bundle();

data.putSerializable("data", 对象比如ArrayList);

 

List<Map<String , String>> list = 

(List<Map<String , String>>)data.getSerializable("data");

//此方法在activity中之间传递List或者map数据集合对象的时候有用

方式二:Bundle.putParcelable(Key, Object);

0 0