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.”

猜你喜欢

转载自blog.csdn.net/qq_41405257/article/details/81746984
今日推荐