Bundle可以传输的数据有哪些

之前明明复习过,结果忘记了,面试的时候只根据实际使用说了String和基本数据类型,后来就说记不太清楚了,之后查了之后也可以传输实现Serializable或Parcelable接口的对象

项目中页面用的fragment,所以两个fragment之间使用了bundle传递数据,数据内容基本都是string类型和int类型,没有很在意这个问题,后期查询,翻了之前总结的面经里面有提到bundle传递的数据类型:string,基本数据类型,和实现serializable或Parcelable接口的对象

- 实现serializable或Parcelable接口的对象

这两个都是实现可序列化的,但是他们的区别在哪里呢

  • serializable接口会创建大量临时对象,需要进行大量垃圾回收

  • serializable界面更容易实现

  • Pracelable比serializable要快

  • Parcelable接口实现需要更多的时间

  • Parcelable中的对象可以通过intent传递

  • 传递serializable对象

// MyObjects instance
MyObjects mObjects = new MyObjects("name", "age", "Address array here");
 
// Passing MyObjects instance via intent
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);


// Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects)   
 mIntent.getSerializableExtra("UniqueKey");

parcelable接口典型实现

 public class MyParcelable implements Parcelable {
     private int mData;
 
     public int describeContents() {
         return 0;
     }
 
     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }
 
     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }
 
         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };
 
     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }

看了这位的

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值