星空网 > 软件开发 > 操作系统

Intent传递对象的两种方法

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

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

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

 

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

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

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

 

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

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

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

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

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

 

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

 

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

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

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

 1 package com.xqx.IntentDemo; 2  3 import java.io.Serializable; 4  5 /** 6  * People实体类,含有 name,sex,age三个属性 ,并实现类的封装 7 */ 8 public class People implements Serializable { 9   private String name;10   private String sex;11   private int age;12 13   public String getName() {14     return name;15   }16 17   public String getSex() {18     return sex;19   }20 21   public int getAge() {22     return age;23   }24 25   public void setName(String name) {26     this.name = name;27   }28 29   public void setSex(String sex) {30     this.sex = sex;31   }32 33   public void setAge(int age) {34     this.age = age;35   }36 }

 

2、传递数据步骤

     //创建Intent对象    Intent intent = new Intent();    intent.setClass(MainActivity.this, NewActivity.class);    //创建实体类    People people = new People();    people.setName("Mark");    people.setSex("boy");    people.setAge(20);    //添加传送数据    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打印

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

 

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

1、实体类实现Parcelable接口

public class Student implements Parcelable {}

重写接口的两个方法

  @Override  public int describeContents() {    return 0;  }   
// 将对象的需要传递的属性 以 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(19);    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日志

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

 




原标题:Intent传递对象的两种方法

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

官方授牌!喜迎21家优质服务商进驻:https://www.kjdsnews.com/a/697026.html
投放的效果是否属于正常水平?品类基准报告帮你解答!:https://www.kjdsnews.com/a/697027.html
亚马逊爆款USB麦克风,美国专利侵权预警!:https://www.kjdsnews.com/a/697028.html
亚马逊无法修改标题怎么解决:https://www.kjdsnews.com/a/697029.html
家居装饰成为eBay热销品类之一!“印度制造”最受欢迎!:https://www.kjdsnews.com/a/697030.html
Shopee卖家注意了!今日起!物流咨询渠道更新:https://www.kjdsnews.com/a/697031.html
品牌出海新机遇,玩转“新兴”与“王牌”主题沙龙成功举办!:https://www.kjdsnews.com/a/1836370.html
每月资讯 | CBD为您带来最新行业动态:https://www.kjdsnews.com/a/1836371.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流