Intent传递对象分为二种方法:
一种是通过实现Serializable接口传递对象、一种是通过实现Parcelable接口传递对象;
实体类 implements 接口 Serializable
(如果类里面还引用了其它类、那么被引用的类也要Serializable,不然会抛
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object
Parcelable同理)
public class people implements Serializable {
private static final long serialVersionUID = 1L;
public String name;
public String age;
}
这里面的serialVersionUID需要注意一下,它的作用是序列化和反序列化时保持版本的兼容性,如果你未指定,运行时也会默认生成,在进行反序列化时只有数据和当前类的serialVersionUID相同是才能够正常的反序列化,你不指定serialVersionUID一般情况下也不会出问题,但是如果当前类发生了改变例如删掉了某个成员变量那么当前类的serialVersionUID也会出现改变,之后你对数据进行反序列化就会出现错误,这里我指定为1L,L为Long数据类型。
Intent传递参数
/*方法1*/
Intent intent = new Intent();
intent.setClass(one.this, two.class);
Bundle bundle = new Bundle();
bundle.putSerializable("user", user);
intent.putExtras(bundle);
this.startActivity(intent);
/*方法2*/
Intent itScan = new Intent(one.this, two.class);
itScan.putExtra("user", user);
startActivity(itScan);
接收数据
User user= new User ();
user= (User ) getIntent().getSerializableExtra("user");
在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。
(转载)具体解决方案
one:http://blog.csdn.net/wei_lei/article/details/50515093
two:http://www.open-open.com/lib/view/open1460377303650.html