android中Parcelable接口的使用

深入解析进化算法中的选择与多样性 在计算科学的世界里,最优雅的解决方案往往源于最古老的智慧——大自然。进化算法(Evolutionary Algorithms, EAs)正是这样一种受达尔文自然选择学说启发的强大工具。它们通过模拟“适者生存”的过程,在庞大的可能性海洋中搜寻最优解。。:指的是深入挖掘和优化已知的优质解。这就像在一片已知的沃土上精耕细作,力求获得最高产量。强大的选择压力会促进这一过程。:指的是在整个搜索空间中寻找全新的、可能更优的解。这好比派遣探险队去发现新大陆,充满了未知与机遇。保持种群的多样性是探索的关键。 阅读详情

android中Parcelable接口的使用

1. 简介

Parcelable是一个接口、用来实现序列化。与此类似的还有一个接口Serializable,这是JavaSE本身支持的,而Parcelable是android特有的。二者比较:

(1)Parcelable使用起来稍复杂点,而后者使用起来非常简单。下面例子中会看到。

(2)Parcelable效率比Serializable高,支持Intent数据传递,也支持进程间通信(IPC)。

(3)Parcelable使用时要用到一个Parcel,可以简单将其看为一个容器,序列化时将数据写入Parcel,反序列化时从中取出。

(4)在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable。Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable在外界有变化的情况下不能很好的保证数据的持续性。尽管Serializable效率低点,但此时还是建议使用Serializable 。

二、源码解析

package android.os;  

/** 
 * Interface for classes whose instances can be written to 
 * and restored from a {@link Parcel}.  Classes implementing the Parcelable 
 * interface must also have a static field called <code>CREATOR</code>, which 
 * is an object implementing the {@link Parcelable.Creator Parcelable.Creator} 
 * interface. 
 *  
 * <p>A typical implementation of Parcelable is:</p> 
 *  
 * <pre> 
 * 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(); 
 *     } 
 * }</pre> 
 */  
public interface Parcelable {  
    /** 
     * Flag for use with {@link #writeToParcel}: the object being written 
     * is a return value, that is the result of a function such as 
     * "<code>Parcelable someFunction()</code>", 
     * "<code>void someFunction(out Parcelable)</code>", or 
     * "<code>void someFunction(inout Parcelable)</code>".  Some implementations 
     * may want to release resources at this point. 
     */  
    public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001;  

    /** 
     * Bit masks for use with {@link #describeContents}: each bit represents a 
     * kind of object considered to have potential special significance when 
     * marshalled. 
     */  
    public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;  

    /** 
     * Describe the kinds of special objects contained in this Parcelable's 
     * marshalled representation. 
     *   
     * @return a bitmask indicating the set of special object types marshalled 
     * by the Parcelable. 
     */  
    public int describeContents();  

    /** 
     * Flatten this object in to a Parcel. 
     *  
     * @param dest The Parcel in which the object should be written. 
     * @param flags Additional flags about how the object should be written. 
     * May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}. 
     */  
    public void writeToParcel(Parcel dest, int flags);  

    /** 
     * Interface that must be implemented and provided as a public CREATOR 
     * field that generates instances of your Parcelable class from a Parcel. 
     */  
    public interface Creator<T> {  
        /** 
         * Create a new instance of the Parcelable class, instantiating it 
         * from the given Parcel whose data had previously been written by 
         * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}. 
         *  
         * @param source The Parcel to read the object's data from. 
         * @return Returns a new instance of the Parcelable class. 
         */  
        public T createFromParcel(Parcel source);  

        /** 
         * Create a new array of the Parcelable class. 
         *  
         * @param size Size of the array. 
         * @return Returns an array of the Parcelable class, with every entry 
         * initialized to null. 
         */  
        public T[] newArray(int size);  
    }  

    /** 
     * Specialization of {@link Creator} that allows you to receive the 
     * ClassLoader the object is being created in. 
     */  
    public interface ClassLoaderCreator<T> extends Creator<T> {  
        /** 
         * Create a new instance of the Parcelable class, instantiating it 
         * from the given Parcel whose data had previously been written by 
         * {@link Parcelable#writeToParcel Parcelable.writeToParcel()} and 
         * using the given ClassLoader. 
         * 
         * @param source The Parcel to read the object's data from. 
         * @param loader The ClassLoader that this object is being created in. 
         * @return Returns a new instance of the Parcelable class. 
         */  
        public T createFromParcel(Parcel source, ClassLoader loader);  
    }  
} 

三、示例

Student类:

package com.home.testparcelable;  

import android.os.Parcel;  
import android.os.Parcelable;  

public class Student implements Parcelable {  
    private int id;  
    private String name;  

    public Student() {  
        super();  
    }  

    public Student(int id, String name) {  
        super();  
        this.id = id;  
        this.name = name;  
    }  

    public int getId() {  
        return id;  
    }  

    public void setId(int id) {  
        this.id = id;  
    }  

    public String getName() {  
        return name;  
    }  

    public void setName(String name) {  
        this.name = name;  
    }  

    @Override  
    public int describeContents() {  
        return 0;  
    }  

    @Override  
    public void writeToParcel(Parcel dest, int flags) {  
        // 序列化过程:必须按成员变量声明的顺序进行封装  
        dest.writeInt(id);  
        dest.writeString(name);  
    }  

    // 反序列过程:必须实现Parcelable.Creator接口,并且对象名必须为CREATOR  
    // 读取Parcel里面数据时必须按照成员变量声明的顺序,Parcel数据来源上面writeToParcel方法,读出来的数据供逻辑层使用  
    public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {  

        @Override  
        public Student createFromParcel(Parcel source) {  
            return new Student(source.readInt(), source.readString());  
        }  

        @Override  
        public Student[] newArray(int size) {  
            return new Student[size];  
        }  
    };  

}  

MainActivity:

package com.home.testparcelable;  

import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  

public class MainActivity extends Activity {  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        Button btn = (Button) findViewById(R.id.main_btn);  
        btn.setOnClickListener(new OnClickListener() {  

            @Override  
            public void onClick(View arg0) {  
                Intent in = new Intent(MainActivity.this, SecondActivity.class);  
                in.putExtra("stu", new Student(1, "李四"));  
                startActivity(in);  
            }  
        });  
    }  
}  

SecondActivity:

package com.home.testparcelable;  

import android.app.Activity;  
import android.os.Bundle;  

public class SecondActivity extends Activity {  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        Student stu = getIntent().getParcelableExtra("stu");  
        System.out.println("id:" + stu.getId());  
        System.out.println("name:" + stu.getName());  
    }  
}  
Atlas 300I Pro推理卡性能深度测试:对比业界同类产品的能效比与算力表现 本文对昇腾Atlas 300I Pro推理卡进行了深度性能评测,重点对比了其与业界同类产品的能效比与算力表现。实测数据显示,该卡在运行BERT等模型时展现出高达39.6句子/秒/瓦的卓越能效比,电力成本可降低40%以上,同时其达芬奇架构和硬件加速特征检索能力,为高密度在线推理和向量检索场景提供了强大支撑。 阅读详情

相关推荐

告别手册!S32K3XX的eMIOS实战:从GPT定时到PWM输出,一个通道搞定电机控制

本文深入探讨了NXP S32K3XX系列MCU的eMIOS模块在电机控制中的高效应用。通过单通道实现PWM生成、输入捕获和定时中断三大功能,展示了如何简化BLDC电机控制硬件设计。文章结合RTD库与寄存器级编程,提供动态模式切换和性能优化方案,显著降低CPU负载。

weixin_42522374的博客 147

Android中Parcelable接口用法总结

1.Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel。Classes implementing the Parcelable interfac...

chuntianlong5227的博客 340

特征金字塔网络(Feature Pyramid Network, FPN)

特征金字塔网络(FPN)是计算机视觉领域的一个里程碑式工作,通过高效的多尺度特征融合,显著提升了目标检测和分割任务的性能。其核心在于自底向上路径、自顶向下路径和横向连接的协同工作,兼顾了高分辨率和强语义信息。FPN的通用性和高效性使其成为许多现代检测框架的标配模块。

Rhett_Butler0922的博客 1727

Android 快速实现Parcelable接口

Android序列化有两种方式, 实现Serializable, 这种直接 implements Serializable就好了. 另一种就是实现Parcelable 需要写一些代码. 好在有android parcelable code generator这款插件, 省去了这个繁琐的过程.下面就来详细看下怎么使用这款插件: 首先安装插件按照提示重启Android studio写Bean类写到这样就

MAIMIHO的博客 2560

等时替代模型( Isotemporal Substitution Model)

文献阅读:Associations of sedentary time and physical activity with adverse health conditions: Outcome-wide analyses using isotemporal substitution model(该论文于2022年4月28日发表在柳叶刀子刊 EClinicalMedicine 上)

姚巨龙的博客 1万+

Dify实战:智能邮件工作流配置全解析

本文详细解析了如何利用Dify平台配置智能邮件工作流,实现邮件处理的自动化与智能化。内容涵盖从获取邮箱授权、安装插件、安全配置环境变量,到构建邮件发送工作流、创建调用该工作流的智能体(Agent)的完整步骤,并分享了连接知识库实现智能客服、定时发送日报等进阶自动化场景,旨在帮助用户显著提升邮件处理效率。

t8u9v0w1x的博客 795

android.os.Parcelable 接口的使用

什么是Parcelable ?      Parcelable,定义了将数据写入Parcel,和从Parcel中读出的接口。一个实体(用类来表示),如果需要封装到消息中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了。   Parcelable 传递对象     Android序列化对象主要有两种方法:     1.实现Serializable接口,实现Serializ

moubenmao的专栏 1903

华为OD机考双机位C卷 - 信道分配(Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 信道分配

算法大师! 453

android 如何使用Parcelable接口

今天,简单讲讲android如何使用Parcelable接口传递数据。 这个其实也很简单,之前我写过博客将关于Serializable接口传递对象,实现Parcelable接口也是为了传递对象,但效率更高,所以在网上查找了相关的资料,这里记录一下。   想要在两个activity之间传递对象,那么这个对象必须序列化,android中序列化一个对象有两种方式,一种是实现Serializable...

谢岩的博客 7320

四足机器人关节电机控制器实现(附C++代码)

本文先对做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章提示:以下是本篇文章正文内容。

qq_35635374的博客 462

升级最新IDEA版本(2022.1.1)

升级最新IDEA版本(2022.1.1)

软件老王 3万+

Android中Parcelable接口的使用

  在做开发的过程中,序列化是非常常见的。比如要将对象保存本地磁盘或者在网络上传输等。实现序列化有两种方式,一种是实现Serializable接口,第二种是实现Parcelable。 Serializable与Parcelable的区别   1、Serializable是JDK提供的接口,而Parcelable是Android SDK提供的。   2、Serializable序列化...

daxiang3255的博客 194

人工智能 (Artificial Intelligence, AI) 原理与代码实例讲解

人工智能 (AI) 机器学习 (Machine Learning) 深度学习 (Deep Learning) 算法 模型 代码实例 应用场景 1. 背景介绍 人工智能 (Artificial Intelligence, AI) 作为一项前沿科技,近年来发展迅速,已渗透到生活的方方面面

AI架构师小马 1230

Android Parcelable接口的使用

Serializable(Java自带): Serializable是序列化的意思,表示将一个对象转换成可存储或可传输的状态。序列化后的对象可以在网络上进行传输,也可以存储到本地。 Parcelable(android 专用): 除了Serializable之外,使用Parcelable也可以实现相同的效果, 不过不同于将对象进行序列化,Parcelable方式的实现原理是将一个完整的对

peiyuWang_2015的博客 474

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Android中使用Parcelable接口

Android中传递复杂类型,主要是将自己的类转换为基础的字节数组,Activity之间传递数据是通过Intent实现的。 Android序列化对象主要有两种方法,实现Serializable接口、或者实现Parcelable接口。 实现Serializable接口是Java本身就支持的,而Parcelable是Android特有的功能,效率比实现Seriali

sinat_31820805的博客 413

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Android 使用Parcelable接口在进程间传递对象

Android在使用Intent打开一个Activity,Service时,发生了进程的切换。如果我们想要将一个自己的对象通过Intent传递给另一个Activity,Service的话,我们就需要用到Parcelable接口。 Parcelable接口的作用是将我们的对象序列化到一个Parcel对象中,Parcel对象中的writeXX()方法是C++实现的,可以将写入的数据写到字节流中。 下面以一个非常简单的demo来展示Parcelable的使用方法:

快乐编程工作室 1880

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Android开发当中Parcelable接口的使用

文本转载于:http://www.2cto.com/kf/201205/132814.html 对于Android来说传递复杂类型,主要是将自己的类转换为基础的字节数组,Activity之间传递数据是通过Intent实现的。 Android序列化对象主要有两种方法,实现Serializable接口、或者实现Parcelable接口。实现Serializable接口是Java  SE本身

阿良的专栏 1749

android 的接口使用场景,Android Parcelable接口使用方法详解

Android Parcelable接口使用方法详解1. Parcelable接口Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelable interface must also have a static field...

weixin_30010773的博客 597

Android ---【经验篇】Serializable 接口与 Parcelable 接口的使用方法和区别,怎么选择?

文章目录一、前言:二、Serializable 接口三、Parcelable 接口四、两种接口怎么选择 一、前言: 本节主要讲解 IPC 中的基本概念,Serializable 接口与 Parcelable 接口。 这两个接口可以完成对象的序列化过程,当我们需要通过 Intent 和 Binder 传输数据时就需要使用 Serializable 与 Parcelable 。还有的时候我们需要把对象持久化到存储设备上或者通过网络传输给其他客户端,这个时候也需要使用这两个接口来完成对象的持久化,下面我们一一来介

梁同学与Android 788

Android 使用Serialiable接口和Parcelable接口进行数据传送

这篇文章主要针对Serialiable和Parcelable接口来传递对象。呈现的功能是跳转到另一个界面,然后通过toast展现我收到的数据。

Rssqzqyp的博客 769
上一篇: 十、多媒体编程
下一篇: google官方的下拉刷新+自定义上拉加载更多
CoderLiLe
博客等级 码龄11年 2粉丝 26原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值