Android Parcelable vs Serializable

Source: Internet
Author: User

Android Parcelable vs Serializable
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
  
    skills = new ArrayList
   
    ();    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
    
      getSkills() {        return skills;    }    public void setSkills(List
     
       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
  
   (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:

Implement the describeContents method. Content interface description. By default, 0 is returned. Implement the writeToParcel method. Transfer Data to the Parcel container. Create an instance of the Parcelable. Creator interface to read data in the Parcel container.

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
  
   
Skills = new ArrayList
   
    
(); 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
    
     
GetSkills () {return skills;} public void setSkills (List
     
      
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
      
        CREATOR = new Parcelable. Creator
       
         () {@ 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
  
   (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:

Serializable is easy to implement and does not require any additional serialization operations. The implementation of Parcelable is complicated, and specific interfaces and object handles need to be implemented. Serializable will generate a large number of temporary variables during serialization, resulting in frequent GC. In contrast, Parcelable has higher performance (10 times better than Serializable ), therefore, we recommend that you use the Parcelable interface when using memory (for example, serializing objects to deliver objects in the network or serializing objects to be transferred between processes. Parcelable has an obvious disadvantage: it cannot be used when you want to store data on a disk (for example, permanently saving objects and saving object byte sequences to local files ), because Parcel is essentially not a general serialization mechanism to better implement object transfer between IPC, changing the underlying implementation of data in any Parcel may make the previous data unreadable, therefore, we recommend using Serializable.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.