Make your pojo parcelable like a cup of tea

I am assuming you all are Android app developers and you all knows about android.os.Parcelable.

Often we all face a common problem about implementing Parcelable to any model class or pojo.  But now few tools and libraries are available to solve this problem and with the help of this we do not have to bother about the complexity of its implementation. Lets have a tour of few of them.

http://www.parcelabler.com/

parcelabler.com

parcelabler.com

This online tool is really a gem for all android developers as this will simply convert your pojo’s into parcelable implemented pojo. Let say I have a class called helloWorld.java

class helloWorld{

private String product;
private double price;


}

I have to paste it into the code section and then need to click the build button at the bottom of code section and boom your parcelable implementation is done with necessary annotations.

class helloWorld implements Parcelable {

private String product;
private double price;



    protected helloWorld(Parcel in) {
        product = in.readString();
        price = in.readDouble();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(product);
        dest.writeDouble(price);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<helloWorld> CREATOR = new Parcelable.Creator<helloWorld>() {
        @Override
        public helloWorld createFromParcel(Parcel in) {
            return new helloWorld(in);
        }

        @Override
        public helloWorld[] newArray(int size) {
            return new helloWorld[size];
        }
    };
}

now just copy it and use it. It’s a must try tool.

http://parceler.org/

http://parceler.org/

Here is another interesting project called Parceler.org . Unlike the above one this is a library which you can include into your android project. Lets take the same class example i.e., helloWorld.java

class helloWorld{

String product;
double price;


}

Now you have to add @Parcel annotation above the class.

@Parcel
class helloWorld{

String product;
double price;

public helloWorld(String product, double price){
   this.product = product;
   this.price = price;
}


}

 Note – do not use private variables when using the default field serialization strategy as it will incur a performance penalty due to reflection.

To marshaling do like the below one

Parcelable wrapped = Parcels.wrap(new helloWorld("pen", 10));

To unmarshaling do the following

helloWorld test = Parcels.unwrap(wrapped);

For more details visit http://parceler.org/.

 

I hope this will be really helpful for all android developers.

Happy coding!!!

Leave a Reply

Your email address will not be published. Required fields are marked *