android之bundle

  • Bundle介绍

Bundle主要用于传递数据:它保存的数据,是以key-value(键值对)的形式存在的。

Activity之间的数据传递经常通过Bundle实现,传递的数据可以是boolean、byte、int、long、float、double、string等基本类型或它们对应的数组,也可以是对象或对象数组。当Bundle传递的是对象或对象数组时,必须实现Serializable或Parcelable接口。

  • 使用方法

写数据的方法

Bundle bundle = new Bundle();  
bundle.putString("name", "博客");  
bundle.putInt("height", 175); 
bundle.putParcelable("ParcelableData", new ParcelableData()); 
mBundle.putSerializable("SeriableData",new SeriableData());  

读数据的方法

Bundle bundle = getIntent().getExtras();    

String name = bundle.getString("name");    
int height = bundle.getInt("height"); 
ParcelableData parcelableData = (ParcelableData) bundle.getParcelableExtra("ParcelableData"); 
SeriableData seriableData = (SeriableData) bundle.getSerializableExtra("SeriableData"); 

- 存在的问题

Bundle的大小有限制,不要使用Bundle传递大容量数据
在stackoverflow里面查阅发现有同行遇到类似的问题:

(1)“The size limit of Intent is still pretty low in Jelly Bean, which is somewhat lower than 1MB (around 900K), so you should always be cautious about your data length, even if your application targets only latest Android versions.”

(2)“As per my experience (sometime ago), you are able to put up to 1MB of data in a Bundleencapsulated inside Intent. I think, this restriction was valid up till Froyo or GingerBread.”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值