Android为intent提供了两种传递对象参数类型的方法

分别需要使实体类实现Serializable接口、Parcelable接口

首先我们要知道,传递对象,需要先将对象序列化

一、那么为什么要对象序列化

1、永久性保存对象,保存对象的字节序列到本地文件中;

2、用过序列化对象在网络中、进程间传递对象;

二、序列化对象什么时候实现Serializable接口,什么时候实现Parcelable接口接口

1、Parcelable不能使用将数据存储在磁盘上,因为Parcelable在外界有变化的情况下不能很好的保存数据的持续性。

   因此在这种情况下,建议使用Serializable

2、在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable类。

并且Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。

----------------------------------------------------------------------------------------------------------

下面看下两种传递对象方式的使用方法

一、实体类继承Serializable接口的方式

1、第一步,将我们需要传送的对象所属的实体类实现Serializable接口

 package com.xqx.IntentDemo;

 import java.io.Serializable;

 /**
* People实体类,含有 name,sex,age三个属性 ,并实现类的封装
*/
public class People implements Serializable {
private String name;
private String sex;
private int age; public String getName() {
return name;
} public String getSex() {
return sex;
} public int getAge() {
return age;
} public void setName(String name) {
this.name = name;
} public void setSex(String sex) {
this.sex = sex;
} public void setAge(int age) {
this.age = age;
}
}

2、传递数据步骤

       //创建Intent对象
Intent intent = new Intent();
intent.setClass(MainActivity.this, NewActivity.class);
//创建实体类
People people = new People();
people.setName("Mark");
people.setSex("boy");
people.setAge();
//添加传送数据
intent.putExtra("people", people);
startActivity(intent);

3、接收数据

     Intent intent = getIntent();
People people = (People) intent.getSerializableExtra("people");
Log.i("DATA_SHOW","name-->"+people.getName()+",sex-->"+people.getSex()+",age-->"+people.getAge());

4、Log打印

-/? I/DATA_SHOW﹕ name-->Mark,sex-->boy,age-->

二、实体类继承Parcelable接口的方式

1、实体类实现Parcelable接口

public class Student implements Parcelable {}

重写接口的两个方法

    @Override
public int describeContents() {
return ;
}

// 将对象的需要传递的属性 以 Parcel parcel.writXxx的形式写出,具体看属性的类型
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(sex);
parcel.writeInt(age);
}

添加一个常量CREATOR(名字大小必须固定),该常量必须实现Parcelable的内部接口:Parcelable.Creator,并实现该接口中的两个方法

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

        @Override
public Student createFromParcel(Parcel source) {
Student student = new Student();
student.name = source.readString();
student.sex = source.readString();
student.age = source.readInt();
return student;
} @Override
public Student[] newArray(int size) {
return new Student[size];
}
};

2、传递数据

     Intent intent = new Intent();
intent.setClass(MainActivity.this, NewActivity.class);
Student student = new Student();
student.setName("Alice");
student.setSex("girl");
student.setAge();
intent.putExtra("student",student);
startActivity(intent);

3、接收数据

     Intent intent = getIntent();
Student student = intent.getParcelableExtra("student");
Log.i("DATA_SHOW","name-->"+student.getName()+",sex-->"+student.getSex()+",age-->"+student.getAge());

4、Log日志

-/? I/DATA_SHOW﹕ name-->Alice,sex-->girl,age-->

Intent传递对象的两种方法的更多相关文章

  1. Intent传递对象的两种方法(Serializable,Parcelable) (转)

    今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcela ...

  2. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  3. [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

    http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...

  4. Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!

    [转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...

  5. Intent传递对象的几种方式

    原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...

  6. 读取xml文件转成List<T>对象的两种方法(附源码)

    读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...

  7. 取xml文件转成List<T>对象的两种方法

    读取xml文件转成List<T>对象的两种方法(附源码)   读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...

  8. 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)

    Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难找到Delphi或Pascal的解决方案.可是却可能找到了一个相关的C++类.本文描述几种在Delphi代码中使 ...

  9. WCF生成客户端代理对象的两种方法的解释

    最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...

随机推荐

  1. Supervisor – 用于 Unix 系统的进程监控工具

    Supervisor 是一个客户端/服务端模式的系统,使用户能够监视和控制 UNIX 操作系统的进程.Supervisor 为你提供一个地方来启动,停止和监视进程.进程可以单独或成组的形式控制.您还可 ...

  2. ShortcutMapper – 热门应用程序的可视化快捷键

    ShortcutMapper 是一个流行应用程序的键盘快捷键映射.该应用程序使用 Ajax 调用来加载键盘和应用程序数据.首先,试图找到一个在线资源,其中列出了每个平台的所有应用程序快捷方式.然后你可 ...

  3. LeetCode-Maximum Product of Word Lengths

    Description: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) ...

  4. Android学习笔记之dispatchTouchEvent和OnInterceptTouchEvent和OnTouchEvent三个方法之间的联系...

    PS:好久没有写博客了,项目正式开始启动了,但是怎么也打不起精神来...可能还是不适应放假留校...这下一年只能回家一次了...伤感...写篇博客舒坦下... 学习内容:   Android中disp ...

  5. python编码声明的位置很重要

    python在3.x版本之前,编码一直是一个很头痛的问题.在代码中如果要使用中文,通常都要在文件的头部注明# -*- coding:utf-8 -*- 这样IDE或者解释器才会智能的转换编码. 这其中 ...

  6. ASP.NET 文件上传类 简单好用

    调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...

  7. 《精通Linux内核必会的75个绝技》知识杂记

    http://www.ibm.com/developerworks/cn/linux/l-cn-utrace/ utrace是为运行态的进程提供trace和debug支持. utrace能做如下事情: ...

  8. 一个python爬虫小程序

    起因 深夜忽然想下载一点电子书来扩充一下kindle,就想起来python学得太浅,什么“装饰器”啊.“多线程”啊都没有学到. 想到廖雪峰大神的python教程很经典.很著名.就想找找有木有pdf版的 ...

  9. ios基础之入门(一)

    最近找到了一个可以接触ios开发的职位,可以系统的学习和练习了.先从最基本的开始: 一.获取控件的两种方式 1)第一种,也是经常使用的一种,通过IBOutlet方式.直接按住control键,将控件和 ...

  10. 设计模式--外观(Facade)模式

    Insus.NET在去年有写过一篇<软件研发公司,外观设计模式(Facade)>http://www.cnblogs.com/insus/archive/2013/02/27/293606 ...