Android Parcelable数据序列化详解

什么是什么是Parcelable

Parcelable是Android sdk提供的用实现于数据序列化的一个接口,不同于Java中的基于磁盘或者网络的Serializable,Parcelable是基于内存的,由于内存的读写速度高于磁盘,因此在Android中跨进程对象传递一般使用Parcelable。

如何使用Parcelable

要想使用Parcelable并不容易,需要编写很多代码,如下:
package com.itfitness.androidparcelabletest;

import android.os.Parcel;
import android.os.Parcelable;

public class BookBean implements Parcelable {
    private int bookId;
    private String bookName;
    private String bookAuthor;
    private String bookPrice;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.bookId);
        dest.writeString(this.bookName);
        dest.writeString(this.bookAuthor);
        dest.writeString(this.bookPrice);
    }

    public BookBean() {
    }

    protected BookBean(Parcel in) {
        this.bookId = in.readInt();
        this.bookName = in.readString();
        this.bookAuthor = in.readString();
        this.bookPrice = in.readString();
    }

    public static final Parcelable.Creator<BookBean> CREATOR = new Parcelable.Creator<BookBean>() {
        @Override
        public BookBean createFromParcel(Parcel source) {
            return new BookBean(source);
        }

        @Override
        public BookBean[] newArray(int size) {
            return new BookBean[size];
        }
    };
}

Parcelable虽然好用但并不好使用,每次使用需要编写这么多代码显然很麻烦,但是因为我们使用的是Android Studio啊,Android Studio中有一个专门用来生成Parcelable代码的插件:Android Parcelable code generator,如下图所示。
img_f66391de69990dcb5519cc483e33d0f2.png
安装完成后重启Android Studio然后编写一个BookBean类,按Alt+Insert会弹出一个窗口。
img_0b43c4c6af1665cbc2f6bcbd8c61a76e.png
选择Parcelable选项,Android Studio会自动生成代码,实现Parcelable接口。
package com.itfitness.androidparcelabletest;

import android.os.Parcel;
import android.os.Parcelable;

public class BookBean implements Parcelable {
    private int bookId;
    private String bookName;
    private String bookAuthor;
    private String bookPrice;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.bookId);
        dest.writeString(this.bookName);
        dest.writeString(this.bookAuthor);
        dest.writeString(this.bookPrice);
    }

    public BookBean() {
    }

    protected BookBean(Parcel in) {
        this.bookId = in.readInt();
        this.bookName = in.readString();
        this.bookAuthor = in.readString();
        this.bookPrice = in.readString();
    }

    public static final Parcelable.Creator<BookBean> CREATOR = new Parcelable.Creator<BookBean>() {
        @Override
        public BookBean createFromParcel(Parcel source) {
            return new BookBean(source);
        }

        @Override
        public BookBean[] newArray(int size) {
            return new BookBean[size];
        }
    };
}

Activity中的传值
MainActivity
 final BookBean bookBean=new BookBean();
        bookBean.setBookId(1);
        bookBean.setBookAuthor("阿萨德");
        bookBean.setBookName("水浒传");
        bookBean.setBookPrice("¥24");
        findViewById(R.id.bt_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                intent.putExtra("testData",bookBean);
                startActivity(intent);
            }
        });
TestActicity
 BookBean bookBean = getIntent().getParcelableExtra("testData");
        Log.e("测试",bookBean.getBookId()+"==="+bookBean.getBookAuthor()+"==="+bookBean.getBookName()+"==="+bookBean.getBookPrice());
img_8aec547a3205fdd27853eb2fad905007.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值