On Android, if you like typing tons of lines of boilerplate for every property in your Parcelable classes, then this article is not for you. So, go about your business citizen.
However, if you would like to create your class with 2 tiny additions and call it a day, you’re gonna love this post!
UPDATE – thanks to the author, John Ericksen, for pointing out that Parceler does not actually create equals and hashCode methods. I have updated the examples and the post to properly reflect this
Parceling is one technique for passing objects from one context to another in Android apps. To pass the objects along, they must implement the Parcelable interface and you’re forced to implement the methods and type all of that boilerplate code in-between. In many cases, your objects may contain tons of properties and that equates to exponential typing. In order to deal with such boilerplate code, we implement rules at Dreamsocket like organizing the properties in alphabetical order because of the requirement to write to and read from the parcels in the same order, and I’m sure you’ve developed your own methods of trying to drudge through copy/pasting the properties in as fast and efficient a manner as possible.
While Android Studio does a great job in helping generate the necessary methods to implement, this solution is a massive waste of valuable time and prone to mistakes.
Introducing Parceler.
Parceler is a code generation library that generates the Android Parcelable boilerplate source code.
In a nut shell, add the @Parcel annotation to your POJO and a blank constructor (UPDATE: a blank constructor is necessary ONLY when another constructor with parameters exists – otherwise, you can drop the constructor completely), and you’re in business. What’s that you say?? Impossible? Take a look at a comparison between these 2 versions of the same object called “Jedi”. The first version implements Parcelable along with all of its methods and the other is a Parceler version of the same object structure:
Parcelable version (64 lines of code):
package com.dreamsocket.parcelertest; import android.os.Parcel; import android.os.Parcelable; public class Jedi implements Parcelable { public String bladeColor; public Long id; public String homePlanet; public boolean hasPadawan; public boolean isPadawan; public boolean isSith; public String master; public String name; public Jedi() {} public static final Parcelable.Creator<Jedi> CREATOR = new Parcelable.Creator<Jedi>() { public Jedi createFromParcel(Parcel in) { return new Jedi(in); } public Jedi[] newArray(int size) { return new Jedi[size]; } }; private Jedi(Parcel p_in){ this.bladeColor = p_in.readString(); this.id = p_in.readLong(); this.homePlanet = p_in.readString(); this.hasPadawan = p_in.readInt() == 1; this.isPadawan = p_in.readInt() == 1; this.isSith = p_in.readInt() == 1; this.master = p_in.readString(); this.name = p_in.readString(); } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.bladeColor); dest.writeLong(this.id); dest.writeString(this.homePlanet); dest.writeInt(this.hasPadawan ? 1 : 0); dest.writeInt(this.isPadawan ? 1 : 0); dest.writeInt(this.isSith ? 1 : 0); dest.writeString(this.master); dest.writeString(this.name); } @Override public int describeContents() { return 0; } }
Parceler version (16 lines of code):
package com.dreamsocket.parcelertest.parceler; import org.parceler.Parcel; @Parcel public class Jedi { public String bladeColor; public Long id; public String homePlanet; public boolean hasPadawan; public boolean isPadawan; public boolean isSith; public String master; public String name; }
Essentially, Parceler is creating a Parcelable wrapper class for use with Parcels.wrap() and Parcels.unwrap() static methods at runtime, while your original class is left as is.
Now, when you’re ready to use Parceler in production, create your Parcelable with Parcels.wrap() method:
Parcelable wrapped = Parcels.wrap(jedi);
and then use Parcels.unwrap() to retrieve your object:
Jedi jedi = Parcels.unwrap(wrapped);
Parceler also offers a level of consistency between developers. For example, a seemingly harmless action of how to read/write booleans can be achieved in several different ways. This stackoverflow example shows how easy it is for a team of developers to approach something this simple and yet come up with several different implementations. Parceler eliminates these types of issues.
For more information on Parceler and it’s many features, head on over to its Github repo and check out the readme
Have a bandit day!
Psychoanalyst Sigmund Freud distinguished female orgasms as clitoral orgasms in young, immature and vaginal women in those with a healthy sexual response. Here’s a list of them:. There are other forms of orgasm that Freud and Dodson largely rule out, but many others have described. newzealandrx.com For example:.
Comment prendre du viagra Cialis 20mg pas cher Sbau associés une hypertrophie bénigne de la prostate. Sildenafil mg prix Prix cialis 20mg. canada casino online Elle est également indiquée chez les hommes ayant des troubles urinaires dûs à une hypertophie de la prostate.
Tweet