Android 序列化(Serializable和Parcelable)

import java.io.Serializable;

public class UserSerializable implements Serializable {

public String name;

public int age;

}

然后你会发现没有serialVersionUID

Android Studio 是默认关闭 serialVersionUID 生成提示的,我们需要打开Setting,执行如下操作:

再次回到UserSerializable类,有个提示,就可以添加serialVersionUID了。

效果如下:

public class UserSerializable implements Serializable {

private static final long serialVersionUID = 1522126340746830861L;

public String name;

public int age = 0;

}

💥 实现Parcelable


Parcelabel的实现,不仅需要实现Parcelabel接口,还需要在类中添加一个静态成员变量CREATOR,这个变量需要实现 Parcelable.Creator 接口,并实现读写的抽象方法。如下:

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 CREATOR

= new Parcelable.Creator() {

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();

}

}

此时Android Studio 给我们了一个插件可自动生成Parcelable 。

🔥 自动生成 Parcelable

==================

public class User {

String name;

int age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

想进行序列化,但是自己写太麻烦了,这里介绍个插件操作简单易上手。

💥 先下载


💥 使用


💥 效果


public class User implements Parcelable {

String name;

int age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeString(this.name);

dest.writeInt(this.age);

}

public void readFromParcel(Parcel source) {

this.name = source.readString();

this.age = source.readInt();

}

public User() {

}

protected User(Parcel in) {

this.name = in.readString();

this.age = in.readInt();

}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

@Override

public User createFromParcel(Parcel source) {

return new User(source);

}

@Override

public User[] newArray(int size) {

return new User[size];

}

};

}

搞定。

写完了咱就运行走一波。

🔥 使用实例

=======

💥 Serializable


MainActivity.class

Bundle bundle = new Bundle();

UserSerializable userSerializable=new UserSerializable(“SCC”,15);

bundle.putSerializable(“user”,userSerializable);

Intent intent = new Intent(MainActivity.this, BunderActivity.class);

intent.putExtra(“user”,bundle);

startActivity(intent);

BunderActivity.class

Bundle bundle = getIntent().getBundleExtra(“user”);

UserSerializable userSerializable= (UserSerializable) bundle.getSerializable(“user”);

MLog.e(“Serializable:”+userSerializable.name+userSerializable.age);

日志:

2021-10-25 E/-SCC-: Serializable:SCC15

💥 Parcelable


MainActivity.class

Bundle bundle = new Bundle();

bundle.putParcelable(“user”,new UserParcelable(“SCC”,15));

Intent intent = new Intent(MainActivity.this, BunderActivity.class);

intent.putExtra(“user”,bundle);

startActivity(intent);

BunderActivity.class

Bundle bundle = getIntent().getBundleExtra(“user”);

UserParcelable userParcelable= (UserParcelable) bundle.getParcelable(“user”);

MLog.e(“Parcelable:”+userParcelable.getName()+userParcelable.getAge());

日志:

2021-10-25 E/-SCC-: Parcelable:SCC15

🔥 Parcelable 中使用泛型

===================

💥 UserParcelable


public class UserParcelable implements Parcelable {

private String name;

private int age;

private T data;

//…set/get部分省略

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

public UserParcelable(String name, int age, T data) {

this.name = name;

this.age = age;

this.data = data;

}

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeString(this.name);

dest.writeInt(this.age);

//这里:先保存这个泛型的类名

dest.writeString(data.getClass().getName());

dest.writeParcelable(this.data, flags);

}

public void readFromParcel(Parcel source) {

this.name = source.readString();

this.age = source.readInt();

//这里

String dataName = source.readString();

try {

this.data = source.readParcelable(Class.forName(dataName).getClassLoader());

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

protected UserParcelable(Parcel in) {

this.name = in.readString();

this.age = in.readInt();

//这里

String dataName = in.readString();

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

成长或者是报班学习,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-3K9yLjwZ-1714948209547)]

[外链图片转存中…(img-K4zDttwZ-1714948209548)]

[外链图片转存中…(img-kXzb7sWH-1714948209548)]

[外链图片转存中…(img-LTHzFZCb-1714948209548)]

[外链图片转存中…(img-VfGK2PQL-1714948209549)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值