惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
Cyberwarzone
Cyberwarzone
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
S
Securelist
Security Latest
Security Latest
T
Tor Project blog
J
Java Code Geeks
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
T
Troy Hunt's Blog
V
Visual Studio Blog
H
Hacker News: Front Page
P
Privacy International News Feed
Jina AI
Jina AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
S
Schneier on Security
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
The Last Watchdog
The Last Watchdog
Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 【当耐特】
博客园_首页
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
T
Threat Research - Cisco Blogs
雷峰网
雷峰网
N
News and Events Feed by Topic
Google DeepMind News
Google DeepMind News
SecWiki News
SecWiki News
C
Cisco Blogs
L
LINUX DO - 最新话题
MongoDB | Blog
MongoDB | Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
K
Kaspersky official blog
小众软件
小众软件
博客园 - 聂微东
D
Docker
The GitHub Blog
The GitHub Blog
IT之家
IT之家
A
Arctic Wolf
L
LINUX DO - 热门话题

博客园 - 蓝风笨笨

简化问题的模式 模板模式和策略模式 为什么要优先使用组合而不是继承 SequenceFile介绍 基于Hadoop Sequencefile的小文件解决方案 Hadoop命令解释 Hadoop源代码分析之Configuration - 蓝风笨笨 onSaveInstanceState和onRestoreInstanceState触发的时机 Android 获取AndroidManifest.xml文件versionCode,versionName属性 . 术语解释 Android开发指南中文版(十四)User Interface-Binding to Data with AdapterView Windows环境下Android Sdk源码下载 Android开发指南中文版(十三)User Interface-Notifications Android开发指南中文版(十二)User Interface-Dialogs Android开发指南中文版(十一)User Interface-Menus Hierarchy Viewer Tool Android开发指南中文版(十)User Interface-Input Events Android开发指南中文版(九)User Interface-XML Layouts Android开发指南中文版(八)User Interface
Serializable 和 Parcelable 区别
蓝风笨笨 · 2012-01-07 · via 博客园 - 蓝风笨笨

android 中自定义的对象的序列化方法:

  • implements Parcelable
  • implements Serializable。
一 序列化原因:
    1. 永久性保存对象,保存对象的字节序列到本地文件中;
    2. 通过序列化对象在网络中传递对象;
    3. 通过序列化在进程间传递对象。
二 至于选取哪种可参考下面的原则:
    1. 在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable。
    2. Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
    3. Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。
三  实现:
    1. Serializable 的实现,只需要继承  implements Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。
    2. 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<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();
     }
 }