Android 序列化之Serializable和Parcelable

默认情况下implements Serializable是不会自动生成UID的,我们可以在设置中开启提示:

这里写图片描述

然后可以看到提示:

这里写图片描述

然后光标放在类名上,快捷键Alt + Enter选中生成serialVersionUID:

这里写图片描述

Done:

这里写图片描述

Android Studio Parcelable代码生成插件

帮我们生成Parcelable代码

这里写图片描述

Settings>Plugins安装插件后重启AS,快捷键Alt + Ins选择Parcelable:

这里写图片描述

例子

BookSerializable.java

package com.zhoumushui.frogweather.bean;

import java.io.Serializable;

public class BookSerializable implements Serializable{

private static final long serialVersionUID = -8361940723643607913L;

private String name;

private float prize;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public float getPrize() {

return prize;

}

public void setPrize(float prize) {

this.prize = prize;

}

public BookSerializable(String name, float prize) {

this.name = name;

this.prize = prize;

}

}

BookParcelable.java

package com.zhoumushui.frogweather.bean;

import android.os.Parcel;

import android.os.Parcelable;

public class BookParcelable implements Parcelable{

private String name;

private float prize;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public float getPrize() {

return prize;

}

public void setPrize(float prize) {

this.prize = prize;

}

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeString(this.name);

dest.writeFloat(this.prize);

}

public BookParcelable() {

}

public BookParcelable(String name, float prize) {

this.name = name;

this.prize = prize;

}

protected BookParcelable(Parcel in) {

this.name = in.readString();

this.prize = in.readFloat();

}

public static final Creator CREATOR = new Creator() {

@Override

public BookParcelable createFromParcel(Parcel source) {

return new BookParcelable(source);

}

@Override

public BookParcelable[] newArray(int size) {

return new BookParcelable[size];

}

};

}

序列化,传递数据

Intent intentSerializableParcelable = new Intent(MainActivity.this,

SerializableParcelableActivity.class);

Bundle bundle = new Bundle();

// Serializable

BookSerializable bookSerializable = new BookSerializable(“Serializable Book”, 9);

bundle.putSerializable(“bundle_serializable”, bookSerializable);

// Parcelable

BookParcelable bookParcelable = new BookParcelable(“Parcelable Book”, 5);

bundle.putParcelable(“bundle_parcelable”,bookParcelable);

intentSerializableParcelable.putExtras(bundle);

startActivity(intentSerializableParcelable);

接收数据,反序列化

Intent intent = getIntent();

Bundle bundle = intent.getExtras();

BookSerializable bookSerializable = (BookSerializable) bundle.getSerializable(“bundle_serializable”);

textResultSerializable.setText(“Serializable.NAME=” + bookSerializable.getName()

  • “,PRIZE=” + bookSerializable.getPrize());

BookParcelable bookParcelable = bundle.getParcelable(“bundle_parcelable”);

textResultParcelable.setText(“Parcelable.NAME=” + bookParcelable.getName()

  • “,PRIZE=” + bookParcelable.getPrize());
运行结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值