Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Parcelable vs Serializable

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 797
    Comment on it

    If we want to send some data over the network or from one place to another place then we have to serialize it first using Serialization in java. Serialization is a process of Marshalling and Unmarshalling of data. Basically the conversion of Objects to bytes called Marshalling and conversion of bytes to original Objects are called Unmarshalling of data.

    Example : I have two activities and I want to send an arraylist from one activity to another activity using Intent so how can we pass that arraylist ?

    We can use either Serializable or Parcelable to send arraylist of objects.

    But which one is better with Android development ?

    Serializable and parcelable both are used for marshalling and Unmarshalling of data, But serilaizable is a marker interface we don't have any method to control this process but Parcelable is better than Serializable interface because this provides methods to control marshalling and unmarshalling of data.

    A Bean class that holds name and class of student and implements Parcelabler :

    private class Bean implements Parcelable{
    
    public String name="";
        public String class="":
    
    public Bean(Parcel source) {
        name = source.readString();
        class = source.readString();
    }
    
    public Bean() {
    }
    
    @Override
    public int describeContents() {
        return this.hashCode();
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(class);
    }
    
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    
        public Bean createFromParcel(Parcel in) {
    
            return new Bean(in);
        }
    
        public Bean[] newArray(int size) {
            return new Bean[size];
        }
    };
    }
    

    First activity to send arraylist using intent

       ArrayList<Bean> arraylist = new ArrayList<Bean>();
       for(int i=0;i<20;i++)
     {
          Bean bean = new Bean();
          bean.name = "name" + i;
          bean.class = "class" + i;
          arraylist.add(bean);
     }
      Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
      intent.putParcelableArrayListExtra("key", arraylist);
      startActivity(intent);        
    

    Second activity to get this arraylist from intent

    private ArrayList<Bean> parcellist;
    parcellist = getIntent().getParcelableArrayListExtra("key");
    

    Note: Process of Masrshalling

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(class);
    }
    

    Note: Process of Unmarshalling

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    
    public Bean createFromParcel(Parcel in) {
    
        return new Bean(in);
    }
    
    public Bean[] newArray(int size) {
        return new Bean[size];
    }
    };
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: