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

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 发送对象。
简单场景:快速实现序列化,无需处理复杂逻辑。

http://www.xdnf.cn/news/2947.html

相关文章:

  • Spring、Spring MVC 与 Spring Boot 的关系与核心用途
  • 什么是全景相机?
  • jenkins slave节点打包报错Failed to create a temp file on
  • Android学习总结之Bitmap篇
  • 《数学物理方程》——第一章 引入与基本概念
  • 【AI工具】DeepWiki试用
  • 第十六届蓝桥杯 C/C++ B组 题解
  • 私有云与虚拟化攻防2(OpenStack渗透场景,大部分云平台都是基于此进行二次开发)
  • C++ 类和对象(3)初始化列表、友元函数、内部类
  • 创龙全志T536全国产(4核A55 ARM+RISC-V+NPU 17路UART)工业开发板硬件说明书
  • 免费IP证书申请
  • leetcode day37 474
  • 【论文阅读】PEEKABOO: Interactive Video Generation via Masked-Diffusion
  • 【神经网络与深度学习】改变随机种子可以提升模型性能?
  • DotNet 入门:(一) 环境安装
  • ElasticSearch入门
  • MYSQL三大日志、隔离级别(MVCC+锁机制实现)
  • 代码颜色模式python
  • 五种机器学习方法深度比较与案例实现(以手写数字识别为例)
  • 生活需要一些思考
  • 设计模式 | 详解常用设计模式(六大设计原则,单例模式,工厂模式,建造者模式,代理模式)
  • 力扣——206.反转链表倒序输出链表
  • Android开发——实现一个计算器
  • Nerfstudio 环境配置与自有数据集(图片和视频)测试全方位全流程实战【2025最新版!!】
  • PyTorch实际上是按照**行优先(Row-Major)**的方式存储数据
  • 基于Arduino的STM32F103RCT6最小系统板的测试及串口通讯
  • 初识Redis · 缓存
  • 网络原理 - 11(HTTP/HTTPS - 2 - 请求)
  • MES系列-ISO95 IEC/ISO 62264
  • 精益数据分析(30/126):电商商业模式的深度剖析与关键指标解读