当前位置: 首页 > news >正文

Android——Serializable和Parcelable

在Android中传递对象的方式

在 Android 开发中,Parcelable 和 Serializable 是两种用于对象序列化的接口

  • Serializable

public class Student implements Serializable {public int id;public String name;public int age;public Student(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}
}

Activity1.java 发送数据

    public void startAction(View view) {Intent intent = new Intent(this, ParcelableActivity.class);Student student = new Student(1, "zs", 12);intent.putExtra("student", student);startActivity(intent);}

Activity2.java 接收数据

    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_parcelable);tv_name = findViewById(R.id.name);Intent intent = getIntent();Student student = (Student) intent.getSerializableExtra("student");if (student != null) {tv_name.setText(student.name);}}
  • Parcelable

  • 实现Parcelable接口
public class StudentParcelable implements Parcelable {public String name;public int age;public StudentParcelable(){}protected StudentParcelable(Parcel in) {name = in.readString();age = in.readInt();}@Overridepublic int describeContents() {return 0;}// 把对象写入到 Parcel对象里面去@Overridepublic void writeToParcel(@NonNull Parcel dest, int flags) {dest.writeString(name);dest.writeInt(age);}public static final Creator<StudentParcelable> CREATOR = new Creator<StudentParcelable>() {// 创建StudentParcelable对象 并且Parcel构建好,传递给我们的StudentParcelable(成员数据就可以从Parcel对象获取了)@Overridepublic StudentParcelable createFromParcel(Parcel source) {return new StudentParcelable(source);}@Overridepublic StudentParcelable[] newArray(int size) {return new StudentParcelable[size];}};
}

Activity1.java 发送数据

    public void startAction(View view) {Intent intent = new Intent(this, SerializableActivity.class);StudentParcelable stu = new StudentParcelable();stu.name = "zssss";stu.age = 12;intent.putExtra("student", stu);startActivity(intent);}

Activity2.java 接收数据

    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_serializable);TextView tv = findViewById(R.id.tv);Intent intent = getIntent();StudentParcelable stu = intent.getParcelableExtra("student");if (stu.name != null) {tv.setText(stu.name);}}

在这里插入图片描述

选择 Parcelable:

高频数据传输:如 Activity 之间传递复杂对象。
性能敏感场景:避免因序列化导致界面卡顿(如大量数据传递)。
Android 组件通信:Intent 中传递自定义对象。

选择 Serializable:

数据持久化:将对象保存到文件或数据库中。
网络传输:通过 HTTP 或 Socket 发送对象。
简单场景:快速实现序列化,无需处理复杂逻辑。

相关文章:

  • C++ 如何计算两个gps 的距离
  • Vue3调度器错误解析,完美解决Unhandled error during execution of scheduler flush.
  • ElasticSearch入门
  • 若依后台管理系统-v3.8.8-登录模块--个人笔记
  • 043-代码味道-循环依赖
  • 健康养生:拥抱活力生活
  • 针对Linux挂载NAS供Minio使用及数据恢复的需求
  • GitHub Actions 自动化部署 Azure Container App 全流程指南
  • [随笔] 升级uniapp旧项目的vue、pinia、vite、dcloudio依赖包等
  • outlook for mac本地邮件存放在哪儿?
  • 【MySQL】聚合查询 和 分组查询
  • Untiy 之如何实现一个跟随VR头显的UI
  • SVMSPro平台获取HTTP-FLV规则
  • Linux0.11系统调用:预备知识
  • docker部署deepseek
  • DDI0487--A1.7
  • 在K8S迁移节点kubelet数据存储目录
  • 对比测评:为什么AI编程工具需要 Rules 能力?
  • 五种机器学习方法深度比较与案例实现(以手写数字识别为例)
  • C#里嵌入lua脚本的例子
  • 俄罗斯纪念卫国战争胜利80周年阅兵式首次彩排在莫斯科举行
  • 美国通过《删除法案》:打击未经同意发布他人私密图像,包括“深度伪造”
  • 企业取消“大小周”引热议,半月谈:不能将显性加班变为隐性加班
  • “85后”潘欢欢已任河南中豫融资担保有限公司总经理
  • 国务院任免国家工作人员:饶权任国家文物局局长
  • 人社部:我国劳动力市场潜力足,韧性强