Intent传递集合实现Serializable或者Parcelable

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。
android上应该尽量采用Parcelable,效率至上

编码上:

Serializable代码量少,写起来方便

Parcelable代码多一些

效率上:

Parcelable的速度比Serializable 高十倍以上

serializable的迷人之处在于你只需要对某个类以及它的属性实现Serializable 接口即可。Serializable 接口是一种标识接口(marker interface),这意味着无需实现方法,Java便会对这个对象进行高效的序列化操作。

这种方法的缺点是使用了反射,序列化的过程较慢。Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。

Parcelable方式的实现原理是将一个完整的对象进行分解,而分解后的每一部分都是Intent所支持的数据类型,这样也就实现传递对象的功能了

实现Parcelable步骤

1)implements Parcelable

2)重写writeToParcel方法,将你的对象序列化为一个Parcel对象,即:将类的数据写入外部提供的Parcel中,打包需要传递的数据到Parcel容器保存,以便从 Parcel容器获取数据

3)重写describeContents方法,内容接口描述,默认返回0就可以

4)实例化静态内部对象CREATOR实现接口Parcelable.Creator

public static final Parcelable.Creator<T> CREATOR
注:其中public static final一个都不能少,内部对象CREATOR的名称也不能改变,必须全部大写。需重写本接口中的两个方法:createFromParcel(Parcel in) 实现从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层,newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话即可(return new T[size]),供外部类反序列化本类数组使用。

简而言之:通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面,在通过createFromParcel从流里读取对象,只不过这个过程需要你来实现,因此写的顺序和读的顺序必须一致。

实现方式

首先创建一个序列化类:EmployeeBean

public class EmployeeBean implements Serializable {

    String employeeName;
    boolean appoint = false;//是否指定
    int employeeId;

    public EmployeeBean() {

    }

    public EmployeeBean(String name ,int id ,boolean appoint) {
        this.employeeId = id;
        this.employeeName = name;
        this.appoint = appoint;
    }

    public boolean getIsAppoint() {
        return appoint;
    }

    public void setAppoint(boolean appoint) {
        this.appoint = appoint;
    }

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }


    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }



}


传递集合

    for (int i = 0; i < 5; i++) {
                    EmployeeBean bean = new EmployeeBean();
                    bean.setEmployeeName("name-->" + i);
                    list.add(bean);
                }
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, TestActivity.class);
                intent.putExtra("bean", (Serializable) list);
                startActivity(intent);//启动intent


#接收方式
List<EmployeeBean> chooseList = (List<EmployeeBean>) getIntent().getSerializableExtra("bean");//获取list方式

传递对象

  EmployeeBean bean1 = new EmployeeBean();
                bean1.setEmployeeName("wangwei");
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, TestActivity.class);
                intent.putExtra("bean", bean1);
                // Bundle bundle = new Bundle();
                // bundle.putSerializable("bean", bean1);//序列化
                // intent.putExtras(bundle);//发送数据
                startActivity(intent);//启动intent

#接收方式
EmployeeBean bean = (EmployeeBean) this.getIntent().getSerializableExtra("bean");

第二种方式 实现Parcelable

public class EmployeeBean implements Parcelable {

    private int id;
    private String department;

    public int getId() {
        return id;
    }

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

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

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

    // 注意这里的write顺序跟下面的read顺序一定要一样
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(department);
    }

    public static final Parcelable.Creator<EmployeeBean> CREATOR = new Creator<EmployeeBean>() {

        @Override
        public EmployeeBean createFromParcel(Parcel source) {
            EmployeeBean teacher = new EmployeeBean();
            teacher.id = source.readInt();
            teacher.department = source.readString();
            return teacher;
        }

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

传递对象

  EmployeeBean teacher = new EmployeeBean();
                teacher.setId(1001);
                teacher.setDepartment("计算机科学与技术");
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
//            Bundle bundle = new Bundle();
//             bundle.putParcelable("parceable", teacher);
                intent.putExtra("parceable", teacher);
                startActivity(intent);


#接受方式
 EmployeeBean bean = (EmployeeBean) getIntent().getParcelableExtra("parceable");

传递集合

 for (int i = 0; i < 5; i++) {
                    EmployeeBean bean = new EmployeeBean();
                    bean.setDepartment("name-->" + i);
                    list.add(bean);
                }
                Intent intent = new Intent(MainActivity.this, TestActivity.class);
                intent.putParcelableArrayListExtra("list", (ArrayList<EmployeeBean>) list);
                startActivity(intent);
#接收方式
  List<EmployeeBean> list = getIntent().getParcelableArrayListExtra("list");//getParcelableArrayListExtra方法
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,820评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,648评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,324评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,714评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,724评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,328评论 1 310
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,897评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,804评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,345评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,431评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,561评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,238评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,928评论 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,417评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,528评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,983评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,573评论 2 359

推荐阅读更多精彩内容