Parcelable – Android passing data between Activities

When I began Android development I had a basic understanding of the Activity life-cycle but I had no understanding how to pass data between these classes. My java instincts told me I could make my data static and pass between that way or I could use Serializable but I had seen and heard horrific lag and memory issues that came with this approach. So I thought there must be a better way, surely Android have accounted for this and indeed they had.

I found Parcelable at first I wasn’t sure of how to implement and resources online where helpful but not for collections of custom objects so I write this in the hope that it can help somebody.

Setting the Scene

Lets say you have a collection of custom objects for this we call our object ball and we have a list of balls List<Ball>, changes to the list of balls are made in Activity A but you want to send them to Activity B.

Let’s Start

Take your data class in this example the Ball class and make it implement Parcelable you will then need to add the following methods to the Ball class or your own data class.

Custom Constructor 

This will be used to create your Data object from the Parcel, so that when Activity B receives the Parcel from Activity A it can convert it to an object. As you can see below this must accept a parcel of data as input and then assign the values you need according example below.

public Ball(Parcel source) {
value = source.readInt();
player = source.readInt();
}

Write to Parcel

This as the name suggests this is used to write the data to the parcel, all you want to do here is write all the values your class need to be assigned in its constructor to the destination parcel as shown below.

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(value);
dest.writeInt(player);
}

Finally add this which tells Parcelable what it should do with this class its pretty self explanatory. Please comment if you want to know more.

public static final Parcelable.Creator<Ball> CREATOR =
new Parcelable.Creator<Ball>() {

@Override
public Ball createFromParcel(Parcel source) {
return new Ball(source);
}

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

Please note you may also have to implement the describeContents method I don’t use this you can if you wish but i typically leave this returning 0.

Sending Activity

Now that you have your data class strapped up and ready to be parceled. In the class that you want to pass your List<Ball> from all you have to do is add it to the intent that you are about to invoke.

Intent activityB = new Intent(v.getContext(),ActivityB.class)

activityB.putParcelableArrayListExtra(“balls”,(ArrayList<? extends Parcelable>) balls);

startActivityForResult(activityB, 0);

Where balls is the list of your ball objects.

Receiving Activity

This is much more simple than you ever expected as now you have already done all the work.

balls = getIntent().getParcelableArrayListExtra(“balls”);

Grab the parcel from the Intent and assign it to your List<Ball> variable in Activity B .

Conclusion

There you have it, Its a simple and effective way of passing data between Activities. Its quick and lightweight which means its easy on memory and reduces the lag or loading times whilst increasing performance. Hopefully someone will find this useful as I think I would have when I was looking.

Any questions or comments, Please ask

Thanks,

Chris

Advertisements

One thought on “Parcelable – Android passing data between Activities

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s