android开发中,我们的Activity之间总避免不了进行数据的传递,几种传递方式大致如下,各有各的用处:
1、Intent携带简单的数据
Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle.putString("username","Mary");
bundle.putInt("age",23);
intent.putExtras(bundle);
参数接收:Bundle bundle=getIntent().getExtras();
String username=bundle.getString("username");
int age=bundle.getInt("age");
2、Intent携带例如ArrayList之类复杂的数据
注意:在传参数前,要用新增加一个List将对象包起来
初始化数据:
Map map=new HashMap();
map.put("aaa","hello");
map.put("bbb","world");
List> list=new ArrayList>();
list.add(map);
Intent intent=new Intent();
Bundle bundle=new Bundle();
//须定义一个list用于在bundle中传递需要传递的List,这个是必须要的