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?)
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.
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.
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'
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.
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(); } }
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(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With