Bundle用于保存键值对信息,常放在intent中实现数据传递,使用方法如下:
1、实例化一个Bundle对象:
Bundle bundle=new Bundle;
2、将键值对数据存放到bundle对象中:
bundle.putString("key_string","value");
bundle.putInt("key_intr",123);
3、将保存完数据的bundle对象保存在intent对象中:
intent.putExtras(bundle);
以上,bundle就成功创建,保存数据并放置在intent中了,接下来将随着intent一起被发送出去,另外一个activity或者其他被intent启动的快件,获取数据的办法是:
4、获取bundle对象:
Bundle bundle=intent.getExtras();
5、获取bundle中的数据:
String value1=bundle.getString("key_String");
Int value2=bundle.getInt("key_int")
以上就是使用bundle传递数据信息的全部流程,前三步在发送数据的组件(activity等)中存放数据,后面两步在被启动的组件(activity等)中获得数据。