Android Parcelable vs Serializable, androidparcelable
Serialization
In Android, objects cannot be directly transmitted between activities as Intent parameters before being serialized.
In Android, Object serialization can be achieved through the Serializable interface or the Parcelable interface. Next, I will introduce how to implement these two interfaces and how to select between them.
Serializable
Serializable is a flag interface, which means that it does not need to be implemented. Because when the object implements the Serializable interface, you only need simple implements.
The sample code is as follows:
package com.example.photocrop.model;import java.io.Serializable;import java.util.ArrayList;import java.util.List;public class PersonSerializable implements Serializable { private static final long serialVersionUID = -1535959029658501338L; private String name; private String sex; private int age; private List<String> skills = new ArrayList<String>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getSkills() { return skills; } public void setSkills(List<String> skills) { this.skills = skills; }}
The code for passing the PersonSerializable in Activity-1 is as follows:
private PersonSerializable generatePerson() { PersonSerializable ps = new PersonSerializable(); ps.setAge(27); ps.setName("wangzhengyi"); ps.setSex("boy"); ps.setSkills(new ArrayList<String>(Arrays.asList("c", "c++", "java", "php"))); return ps; } private void startActivity2() { PersonSerializable ps = generatePerson(); Intent intent = new Intent(this, Activity2.class); intent.putExtra("person", ps); startActivity(intent); }
Receive the PersonSerializable object in activity-2:
Intent intent = getIntent();PersonSerializable ps = (PersonSerializable)intent.getSerializableExtra("person");Log.e("wangzhengyi", "person name :" + ps.getName());Log.e("wangzhengyi", "persion age :" + ps.getAge());
Parcelable
The following steps are required to implement the Parcelable interface:
The sample code is as follows:
Package com. example. photocrop. model; import java. util. arrayList; import java. util. list; import android. OS. parcel; import android. OS. parcelable; public class PersonParcelable implements Parcelable {private String name; private String sex; private int age; private List <String> skills = new ArrayList <String> (); public String getName () {return name;} public void setName (String name) {this. name = name ;} Public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public List <String> getSkills () {return skills;} public void setSkills (List <String> skills) {this. skills = skills ;}@ Override public int describeContents () {return 0 ;}@ Override public void writeToParcel (Parcel dest, int Flags) {dest. writeString (getName (); dest. writeString (getSex (); dest. writeInt (getAge (); dest. writeList (getSkills ();} public void readFromParcel (Parcel source) {setName (source. readString (); setSex (source. readString (); setAge (source. readInt (); source. readList (skills, List. class. getClassLoader ();} public static final Parcelable. creator <PersonParcelable> CREATOR = new Parcelable. creator <Pe RsonParcelable> () {@ Override public PersonParcelable createFromParcel (Parcel source) {// read the transmitted data value from the Parcel container and encapsulate it into a Parcelable object to return to the logic layer. PersonParcelable pp = new PersonParcelable (); pp. readFromParcel (source); return pp ;}@ Override public PersonParcelable [] newArray (int size) {// create an array of the T type and size length, return new T [size. The method is used for external class deserialization of this class array. Return new PersonParcelable [size] ;}};}
Pass the PersonParcelable object in activity-1:
private PersonParcelable generatePerson() { PersonParcelable pp = new PersonParcelable(); ps.setAge(27); ps.setName("wangzhengyi"); ps.setSex("boy"); ps.setSkills(new ArrayList<String>(Arrays.asList("c", "c++", "java", "php"))); return pp; } private void startActivity2() { PersonParcelable pp = generatePerson(); Intent intent = new Intent(this, Activity2.class); intent.putExtra("person", ps); startActivity(intent); }
Receive the PersonParcelable object in activity-2:
Intent intent = getIntent(); PersonParcelable pp = (PersonParcelable) intent.getParcelableExtra("person"); Log.e("wangzhengyi", "person name :" + pp.getName()); Log.e("wangzhengyi", "persion age :" + pp.getAge()); for (String skill : pp.getSkills()) { Log.e("wangzhengyi", "skill:" + skill); }
Differences
The main differences are as follows:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.