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();
//这里
最后
其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。
上面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,上面只是以图片的形式给大家展示一部分。
【Android思维脑图(技能树)】
知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。
【Android高级架构视频学习资源】
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
复习到了哪个阶段就好。
上面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,上面只是以图片的形式给大家展示一部分。
【Android思维脑图(技能树)】
知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。
[外链图片转存中…(img-8QWl0s83-1714044261043)]
【Android高级架构视频学习资源】
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!