android bundle存放数据详解

转载自:android bundle存放数据详解

正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中

例如:

将数据存放倒Intent中传递:

将数据放到Bundle中传递:

但是Intent或者Bundle存放的数据类型是有限的

我想大家都遇到过这个问题,无法将Map、List<Map<String,Object>>等类型数据存放到Bundle或者Intent中

但是大家是否注意到,Bundle或者Intent允许存放对象数据

我们可以从这点着手,我们只要将需要存放到数据先存到一个对象中,再将这个对象存放到Bundle或者Intent中,我们就能成功将想传递到数据传递过去

操作步骤:

新建一个类

将数据存放到对象中,再将对象放到Bundle中进行传递

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle = new Bundle();
Map<String,Objects> map = new HashMap<String,Objects>();
map.put("wenhou","你好");
map.put("name", "jason");
map.put("age", 25);
Data data = new Data();
data.setMap(map);
bundle.putSerializable("data",data);
intent.putExtras(bundle);
startActivity(intent);
 
 
原文地址:https://www.cnblogs.com/mukekeheart/p/5786610.html