activity传递List方法对比

废话不说,刚写了代码实测activity之间传递List<Object>常用方法数据。

以下实测均传递如下对象,

public class Person3 {
    private String name;
    private int age;
    private Boolean isBoy;
}

1.Parcelable序列化1000条1ms,反序列化1000条10ms。

2.Serializable序列化100条1ms(序列化1000条奔溃,android.os.TransactionTooLargeException),反序列化100条70ms。

3.Gson序列化1000条82ms,反序列化1000条30ms。

从实测数据可以看出,时间效率最高的是Parcelable。

至于空间效率,或者说TransactionTooLargeException产生的原因,官方文档

The Binder transaction failed because it was too large.

During a remote procedure call, the arguments and the return value of the call are transferred as Parcel objects stored in the Binder transaction buffer. If the arguments or the return value are too large to fit in the transaction buffer, then the call will fail and TransactionTooLargeException will be thrown.

The Binder transaction buffer has a limited fixed size, currently 1Mb, which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size.

简而言之就是Intent传的数据太大,Binder多个进程共享1M空间,所以理论上只有传递的数据越小才越安全,而没有所谓安全值。如果真的有太大数据需要传递,应该先将序列化的数据保存到本地(如使用SP)。

 结论,Android日常最佳方法是使用Parcelable,但是切记传递值越小越好,如果有大值需要传递请保存本地传递。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值