实现Parcelable

实现ParcelableActivity 之间通过 intent 传递 object 时 该 object 的 class 需要实现 Parcelable nbsp Interfacefor nbsp Parcel Classesimple

Activity之间通过intent传递object时,该object的class需要实现Parcelable。

 

Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a non-null static field called CREATOR of a type that implements the Parcelable.Creator interface.

A typical implementation of Parcelable is:

public class MyParcelable implements Parcelable { private int mData; public int describeContents() { //一般返回0即可 return 0; } public void writeToParcel(Parcel out, int flags) { out.writeInt(mData); } public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() { 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(); } }

 http://developer.android.com/reference/android/os/Parcelable.html

 

最核心的地方是

private MyParcelable(Parcel in)

writeToParcel(Parcel out, int flags)

读写顺序要一致。

 

public class Bill implements Parcelable { Integer fee; String title; Boolean result; public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { out.writeInt(fee); out.writeString(title); out.writeByte((byte) (result!=null && result ? 1 : 0)); } public static final Parcelable.Creator<Bill> CREATOR = new Parcelable.Creator<Bill>() { public Bill createFromParcel(Parcel in) { return new Bill(in); } public Bill[] newArray(int size) { return new Bill[size]; } }; private Bill(Parcel in) { fee = in.readInt(); title = in.readString(); result = (in.readByte()==1); } }

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232847.html原文链接:https://javaforall.net

(0)
上一篇 2025年9月6日 上午10:01
下一篇 2025年9月6日 上午10:22


相关推荐

  • vue中keep-alive、activated的探讨和使用「建议收藏」

    vue中keep-alive、activated的探讨和使用「建议收藏」在修改公司的一个项目的时候发现了activated这个东西,一直觉得很疑惑,之前也没怎么用过啊!官网的生命周期那也没说过这东西啊!生命周期不就createmountupdate和destory这几个东东么,怎么多了个activate出来。百思不得其解,于是去问了下度娘和查了下文档!恍然大悟,原来这东东是结合keep-alive这东东使用的,下面顺便记录一下。 keep-ali………

    2025年8月24日
    3
  • 实战指南:通过 WSL 在 Windows 上部署 OpenClaw (2026版)

    实战指南:通过 WSL 在 Windows 上部署 OpenClaw (2026版)

    2026年3月13日
    1
  • Keil(MDK-ARM)介绍、下载、安装与注册[通俗易懂]

    Keil(MDK-ARM)介绍、下载、安装与注册[通俗易懂]推荐分享一个大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来!http://www.captainbed.net/strongerhuang推荐在我的公众号「strongerHuang」或网站(www.strongerhuang.com)阅读以下教程:Keil系列教程01_Keil介绍、下载、安装与注册Keil系列教程02_新建基础软件…

    2022年6月10日
    60
  • PLSQL安装配置

    PLSQL安装配置安装配置 PLSQL1 官网下载 oracleclient 下载地址 https www oracle com database technologies instant client downloads html 这里以 11 2 xx 版本为示例 解压缩后 找到文件夹 instantclien 11 2 创建文件夹目录 NETWORK 及子目录 ADMIN 并且在目录 ADMIN 新建文件 tnsnames oratnsnames ora 内容示例 具体配置根据实际情况自定义 ORCL

    2026年3月18日
    2
  • 光伏发电功率预测_分布式光伏并网接入电压

    光伏发电功率预测_分布式光伏并网接入电压Reviewonprobabilisticforecastingofphotovoltaicpowerproductionandelectricityconsumption—论文阅读(1)。

    2022年10月19日
    4
  • linux c 报错 warning: large integer implicitly truncated to unsigned type[-Woverflow]

    linux c 报错 warning: large integer implicitly truncated to unsigned type[-Woverflow]警告的原因是:整数溢出整数溢出:当整数达到它所能表述的最大值时,会重新从起点开始#include<stdio.h>intmain(void){ unsigneda=12345678910; printf(“a=%d\n”,a); return0;}该程序输出以后并不是输出a=12345678910而是:上面的代码还不足以说明清楚下面才是重头戏:#include<stdio.h>intmain(void){ inta=2

    2022年7月25日
    26

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号