从一个Activity中传递数据到另一个Activity中,有如下几个方法:

Intent
Bundle
Serializable
Parcelable

首先设置一个Button,按下即可跳转到下一个Activity;

方法1.intent.putExtra传递一些简单的数据:

String s = "hello";
//目前Activity→目标Activity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
//加入intent
intent.putExtra("text",s);
//启动
startActivity(intent);

在SecondActivity里接收数据:

Intent intent = getIntent();
String s = intent.getStringExtra("text");
Log.d("Laze",s);

开始测试,点击Button后看到日志:


传递成功!

putExtra可以传递多种参数:



接收方法名称不一样


但是有规律,get+[传递类型]+Extra,非常简单。

方法2.Bundle对象传递

将要传入的值写入Bundle,再用intent.putExtras把Bundle传入:

String s = "hello,world!";
Bundle bundle = new Bundle();
bundle.putString("data",s);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);

接收时,先从Intent中取出Bundle,再从Bundle中取出String(或其他类型参数):

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String s = bundle.getString("data");
Log.d("Laze",s);

Intent传值和Bundle的区别

Bundle在存取数据是比较灵活的,而Intent在存取数据的种类很少且没有指定数据类型;

想对数据进行比较灵活的操作如批量操作的话就用Bundle;

Bundle是可以对对象进行操作的,而Intent不可以。Bundle相对于Intent比较偏下层,比Intent接口更多,更灵活,但Bundle仍需要借助Intent才能在Activity之间传递。

概括一下,Intent旨在数据传递,bundle旨在存取数据,当然Intent也提供一部分数据的存取,但比起Bundle就显得很不灵活。

当然我还是喜欢用Intent,传简单数据的时候书写量就少了啊 orz

用Bundle传递如ArrayList之类复杂的数据

这种原理是和上面一种是一样的,只是要注意下。 在传参数前,要用新增加一个List将对象包起来。

Map map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
List> list = new ArrayList<>();
list.add(map);
Bundle bundle = new Bundle();
//须定义一个list用于在budnle中传递需要传递的ArrayList,这个是必须要的
ArrayList bundlelist = new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);

接收时:

Bundle bundle = getIntent().getExtras();

ArrayList list = bundle.getParcelableArrayList("list");

List> list1 = (List>)list.get(0);

Log.d("Laze",list1.get(0).toString());

结果:


成功。

方法3.通过Serializable接口

利用Java语言本身的特性,通过将数据序列化后,再将其传递出去。

HashMap map = new HashMap();
map.put("key1","value1");
map.put("key2","value2xxx");
Bundle bundle = new Bundle();
bundle.putSerializable("serializable",map);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);

接收:

Bundle bundle = getIntent().getExtras();
HashMap map = (HashMap)bundle.getSerializable("serializable");
Log.d("Laze",map.toString());

查看日志:


再次成功~

方法4.Parcelable接口

这个是通过实现Parcelable接口,把要传的数据打包在里面,然后在接收端自己分解出来。这个是Android独有的,在其本身的源码中也用得很多,效率要比Serializable相对要好。

但是他代码量相对上面3种要多得多,对新手来说当然是选择Serializable,眼尖的童鞋能看到上面也用到了点:putParcelableArrayList,getParcelableArrayList。

Parcelable的使用,这里是我在用我在用Serializable时遇到了警告于是使用了Parcelable。

总结

平时使用最多的还是直接用Intent直接传值,这样可以少写两行代码(屈服于代码量),必要的时候就会用其他的方法。当我需要传值的时候一般都是在一个list里面,然后就用到Parcelable。

代码简写:

Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
//写为
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
//写为
Bundle bundle = getIntent().getExtras();

这样就舒服多了