Generic Parcelable Implementation

Today I’m going to post you about a generic implementation of Parcelable interface in Android.


import android.os.Parcel;
import android.os.Parcelable;

public class GenericParcelable<T> implements Parcelable {

	private T mValue;
	private static ClassLoader mClassLoader;

	public int describeContents() {
		return 0;

	public void writeToParcel(Parcel parcelOut, int flags) {

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

        public GenericParcelable createFromParcel(Parcel in) {
            return new GenericParcelable(in);

        public GenericParcelable[] newArray(int size) {
            return new GenericParcelable[size];

    public GenericParcelable(T value){
    	this.mValue = value;
    	if(this.mValue != null)
    		GenericParcelable.mClassLoader = value.getClass().getClassLoader();

	private GenericParcelable(Parcel parcelIn) {
            //reading the passed value
    		mValue = (T)parcelIn.readValue(GenericParcelable.mClassLoader);
    	catch(Exception e){

    //generic method of obtaining the parcel value
	public T getValue(){
		return (T) mValue;



This is the way of adding a value to Parcelable object in an Activity class.

Intent intent = new Intent(this, NextActivity.class);
//Create the Parcelable object with an int value
intent.putExtra("myKey", new GenericParcelable<Integer>(15));

Obtaining the value in a different Activity.

int passedValue;
GenericParcelable<Integer> genericParcelable = getIntent().getParcelableExtra("myKey");
if (genericParcelable != null){
   passedValue = genericParcelable .getValue();

Happy coding 🙂