Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parcelable and inheritance in Android

I got an implementation of Parcelable working for a single class that involves no inheritance. I have problems figuring out the best way to implement the interface when it come to inheritance. Let's say I got this :

public abstract class A {     private int a;     protected A(int a) { this.a = a; } }  public class B extends A {     private int b;     public B(int a, int b) { super(a); this.b = b; } } 

Question is, which is the recommended way to implement the Parcelable interface for B (in A? in both of them? How?)

like image 247
Vincent Mimoun-Prat Avatar asked Oct 29 '10 06:10

Vincent Mimoun-Prat


People also ask

What is a Parcelable in Android?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.

What is difference between serialization and Parcelable in Android?

Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations. Parcelable is an Android specific interface where you implement the serialization yourself.

What is Parcelable in Android kotlin?

The kotlin-parcelize plugin provides a Parcelable implementation generator. To include support for Parcelable , add the following Gradle plugin to your app's build.gradle file: plugins { id 'kotlin-parcelize'

Why Parcelable is faster than serializable?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.


2 Answers

Here is my best solution, I would be happy to hear from somebody that had a thought about it.

public abstract class A implements Parcelable {     private int a;      protected A(int a) {         this.a = a;     }      public void writeToParcel(Parcel out, int flags) {         out.writeInt(a);     }      protected A(Parcel in) {         a = in.readInt();     } }  public class B extends A {     private int b;      public B(int a, int b) {         super(a);         this.b = b;     }      public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {         public B createFromParcel(Parcel in) {             return new B(in);         }          public B[] newArray(int size) {             return new B[size];         }     };      public int describeContents() {         return 0;     }      public void writeToParcel(Parcel out, int flags) {         super.writeToParcel(out, flags);         out.writeInt(b);     }      private B(Parcel in) {         super(in);         b = in.readInt();     } } 
like image 165
Vincent Mimoun-Prat Avatar answered Sep 22 '22 02:09

Vincent Mimoun-Prat


This is my variant. I think it's nice because it shows the symmetry between the virtual read- and write- methods very clearly.

Side note: I think Google did a really poor job at designing the Parcelable interface.

public abstract class A implements Parcelable {     private int a;      protected A(int a) {         this.a = a;     }      public void writeToParcel(Parcel out, int flags) {         out.writeInt(a);     }      public void readFromParcel(Parcel in) {         a = in.readInt();     } }  public class B extends A {     private int b;      public B(int a, int b) {         super(a);         this.b = b;     }      public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {         public B createFromParcel(Parcel in) {             return new B(in);         }          public B[] newArray(int size) {             return new B[size];         }     };      public int describeContents() {         return 0;     }      public void writeToParcel(Parcel out, int flags) {         super.writeToParcel(out, flags);         out.writeInt(b);     }      public void readFromParcel(Parcel in) {         super(in);         b = in.readInt();     } } 
like image 44
osxdirk Avatar answered Sep 21 '22 02:09

osxdirk