Wednesday, June 16, 2010

Android Parcelable Example


Few days back I had a requirement to send ArrayList of my Custom Class/Objects which need to be sent through Intent, I guess most of you also find this requirement now and then. I never thought it can be that tricky. 
There are built in functions in Intent to send ArrayList of primitive objects e.g. String, Integer, but when it comes to Custom Data Handling Objects, BOOM… you need to take that extra pain…..

Android has defined a new light weight IPC (Inter Process Communication) data structure called Parcel, where you can flatten your objects in byte stream, same as J2SDK’s Serialization concept.

So let’s come back to my original requirement, I had a Data Handling class, which groups together a set of information-
public class ParcelData {
      int id;
      String name;
      String desc;
      String[] cities = {"suwon", "delhi"};
}

I want an ArrayList<ParcelData> of Data Handling objects to be able to pass through Intent. To do that, I can’t use the ParcelData as it is; you need to implement an Interface android.os.Parcelable which will make Objects of this class Parcelable. So you need to define your data handling class as-

public class ParcelData implements Parcelable {
      int id;
      String name;
      String desc;
      String[] cities = {"suwon", "delhi"};
}

You need to overwrite2 methods of android.os.Parcelable Interface-
describeContents()- define the kind of object you are going to Parcel, you can use the hashCode() here.

writeToParcel(Parcel dest, int flags)- actual object serialization/flattening happens here. You need to individually Parcel each element of the object.
public void writeToParcel(Parcel dest, int flags) {
      Log.v(TAG, "writeToParcel..."+ flags);
      dest.writeInt(id);
      dest.writeString(name);
      dest.writeString(desc);
      dest.writeStringArray(cities);
}

Note: Don’t try things like, dest.writeValue(this) to flatten the complete Object at a time (I don’t think all people have some weird imaginations like me, so you’ll never try this, right ?…) that will end up in recursive call of writeToParcel().

Up to this point you are done with the required steps to flatten/serialize your custom object.

Next, you need to add steps to un-marshal/un-flatten/de-serialize (whatever you call it) your custom data objects from Parcel. You need to define one weird variable called CREATOR of type Parcelable.Creator. If you don’t do that, Android framework will through exception-

Parcelable protocol requires a Parcelable.Creator object called CREATOR

Following is a sample implementation of Parcelable.Creator<ParcelData> interface for my class ParcelData.java-
/**
 * It will be required during un-marshaling data stored in a Parcel
 * @author prasanta
 */
public class MyCreator implements Parcelable.Creator<ParcelData> {
      public ParcelData createFromParcel(Parcel source) {
            return new ParcelData(source);
      }
      public ParcelData[] newArray(int size) {
            return new ParcelData[size];
      }
}

You need to define a Constructor in ParcelData.java which puts together all parceled data back-
/**
       * This will be used only by the MyCreator
       * @param source
       */
      public ParcelData(Parcel source){
            /*
             * Reconstruct from the Parcel
             */
            Log.v(TAG, "ParcelData(Parcel source): time to put back parcel data");
            id = source.readInt();
            name = source.readString();
            desc = source.readString();
            source.readStringArray(cities);
      }

You can deserve some rest now…...as you are done with it. So now you can use, something like-

Intent parcelIntent = new Intent(this, ParcelActivity.class);
ArrayList<ParcelData> dataList = new ArrayList<ParcelData>();
/*
 * Add elements in dataLists e.g.
 * ParcelData pd1 = new ParcelData();
 * ParcelData pd2 = new ParcelData();
 *
 * fill in data in pd1 and pd2
 *
 * dataLists.add(pd1);
 * dataLists.add(pd2);
 */
parcelIntent.putParcelableArrayListExtra("custom_data_list", data);

So, how hard is that? :-)

62 comments:

  1. good example......Thanks
    - santhana

    ReplyDelete
  2. Very well explained....thankyou!! :)

    One more thing wat if I have a Parceable object that has List instanceVariable?

    ReplyDelete
  3. Hi Anandram,
    if that list instance variable contains premitive objects, you don't have to do anything special i.e. probably need to call writeList() to write to parcel.

    But if the parcelable object contains List of Custom objects, you need to make your Custom Class also Parcelable and need to use-
    writeTypedList()

    and
    readList(listInstance, .class.getClassLoader());

    Hope this helps.

    ReplyDelete
  4. Hi really a nice article. Good work. Thank you :)

    ReplyDelete
  5. this is really helpful.. in fact i was very much confused initially.. thanks a lot for posting this wonderful tutorial..

    ReplyDelete
  6. Excellent - i tried to write my parcelable by following the SDK doc, but it was riddled with uncertainty. I followed your technique and had it written in 15 minutes and debugged in 5.

    You might want to include a non-native type like Date and null member data. I had a class similar to yours with an uninitialized date.

    ReplyDelete
  7. Hi Bob,
    appreciate your comments.

    Probably I'll write Part 2 of this which will include a set of advance data structures and your custom classes.

    Let's see when I get time to do that :-)
    Thanks,
    Prasanta

    ReplyDelete
  8. Hi prashant

    i gotta pass an object in intent it is of Messgae[] type(i guess you know this). i did whatever you said and the only change was source.writeArray(Object[] val) where am passing my object of Message[] type. Now, what code to be overwritten on the other side to handle this.Please respond asap

    ReplyDelete
  9. Hi Vivek,
    I guess you are talking about android.os.Message.
    Message internally implements Parcelable, so you need not to worry for its internal data marshaling/un-marshaling.
    If you are using an array of Message[] msgs-
    to write-
    dest.writeArray(msgs);

    to read-
    source.readArray(ParcelData..class.getClassLoader());

    I hope this will work.
    Thanks,
    Prasanta

    ReplyDelete
  10. I'm having trouble recovering the data in the other activity:

    //onCreate
    ArrayList listaCadastradaArray = (ArrayList) getIntent().getParcelableArrayExtra("listaCadastrada");

    it says it can't cast from Parcelable[] to ArrayList

    ReplyDelete
  11. Roger: if you remove the cast your code should work, I had the same problem. I guess you can directly assign the parcelable arraylist to your custom arraylist...

    ReplyDelete
  12. Thanks for sharing, Prasanta!

    Cheers,
    Torsten

    ReplyDelete
  13. Hey Prasanta Paul,

    I appreciate your help.

    Cheers,
    Ritesh

    ReplyDelete
  14. Hi, at first i would like to thank you for your example! At second i have a little problem with recovering the data from intent. Could you please tell me, where is mistake?
    In activity A I do this:
    ArrayList tmpRecordAll = getAllRecords();
    Intent intentAllData = new Intent(DATA_BROADCAST);
    intentAllData.putParcelableArrayListExtra("AllRecordsByID", tmpRecordAll);
    sendBroadcast(intentAllData);
    In actvity B I have this:
    ArrayList tmpArrayListID = intent.getParcelableArrayListExtra("AllRecordsByID");
    and after that the tmpArrayListID is empty...
    thanks for help.

    ReplyDelete
  15. Sorry for my previous comment. Everthing is working fine, error was elsewere :)

    ReplyDelete
  16. I there some method in Parcel so I can write a TreeMap in a Parcel?

    ReplyDelete
  17. How do you handle a nested complex object inside ParcelData i.e. ArrayList where T is a complex object? How would the writeToParcel look?

    ReplyDelete
  18. Where is MyCreator used in the scheme of things?

    ReplyDelete
  19. wow. i like the way you put these tiny puzzle-pieces together with that very simplistic and concise explanation. i have a little query though.

    how would you actually know which string is which? for example, like in your class, you will have multiple similar variables, let's say you have 5 strings..how would you know that this is the 'name' string or this is the 'gender' string..or things like that, when the way to retrieve them is only through the method readString()? would you have to retrieve them in a sequence exactly similar to how you wrote them? this is confusing.

    thats the only little missing piece in this mind-puzzle that i have inside my head about this parcelable interface..

    thank you so much sir. :)

    could anybody help me? ^^

    ReplyDelete
  20. This post - http://idlesun.wordpress.com/2011/07/15/android-parcelable-example-2-sub-object-and-list/ answered my questions. He has an example 1 post out there as well.

    ReplyDelete
  21. Just to echo what Vivek asked, I'm looking to pass a Date. How would I go about doing that?

    ReplyDelete
  22. Excellent example! Thank you!

    ReplyDelete
  23. "static final: must used for CREATOR cariable

    public static final Parcelable.Creator CREATOR=...
    in parceldata.java

    ReplyDelete
  24. thanx a lot Prasanta .. u r an angel :)
    -Ash

    ReplyDelete
  25. Nice One. You helped me at a critical situation

    ReplyDelete
  26. Very useful articular for me
    Thanks a lot
    dharmendra.sahu09@gmail.com

    ReplyDelete
  27. Hi ,
    Is it possible to pass a pointer to structure in binders between proxy and native .

    Regards,
    Raj

    ReplyDelete
  28. Great Job.. thanks!!

    ReplyDelete
  29. Prasanta, thanks for writing this. Very helpful.

    ReplyDelete
  30. Hi Prasantha,
    is it possible for us to pass the socket, inputstream and output stream the same way ? Kindly give a thought in this.
    I've a requirement to pass the bluetooth socket from one activity to the other. But I don't know how to do that. Kindly share some sample code if you have any.

    Thanks
    Sathish

    ReplyDelete
  31. Hi prasantha,

    Is it possible to parcel a date and Map> if possible please let me know

    ReplyDelete
  32. Hi Prasantha,

    This example is really good.

    thanks.

    ReplyDelete
  33. I found this article very useful and hope that it will be very helpful for those who are new to Android.

    Here I would like to ask one question that is it necessary to maintain order while UN-marshaling data i.e, one has to read the data in the same order in which the data has written to the parcel ?

    ReplyDelete
  34. Thanks man! I like the way you explain it and sahre your emotions with us!
    best rgrads from Brazil!

    ReplyDelete
  35. Thanks for sharing this knowledge.

    ReplyDelete
  36. Good Article !!!...Post more dude!!

    ReplyDelete
  37. how to move multiple objects within two activities?

    ReplyDelete
  38. how to move multiple objects within two activities?

    ReplyDelete
  39. Parcelable doesn't seem to work for a little more complicated object, e.g. a Parcelable with recursive call of its ownself, that means a Parcelable has a ListArray of dynamical size inside, e.g:

    public class ParcelData implements Parcelable {
    int id;
    String name;
    String desc;
    String[] cities = {"suwon", "delhi"};

    ListArray mSubItemList;
    }

    and by calling

    private ParcelData( Parcel in ) {
    id = in.readInt();
    name = in.readString();
    desc = in.readString();
    in.readStringArray(cities);

    // now the problem, note: the dim info has already been written into Parcel
    // by using writeToParcel()
    int numberOfSubitems = in.readInt();
    if( numberOfSubitems > 0 ) {
    mSubItemList = new ListArry();
    for( int i = 0; i < numberOfSubitems; i++ ) {
    mSubItemList.add( new ParcelData(in) );
    }
    }
    else {
    mSubItemList = null;
    }
    // it causes exception, where is the problem???
    }


    Steven

    ReplyDelete
  40. Thanks for the code!

    ReplyDelete
  41. Hello,
    I want to get the location reminder whenever we are going to one place to another place when it reach that destination alert dialog is display in background using service.

    ReplyDelete
  42. Hello sir..:)
    Prateek here.
    Nice example.

    ReplyDelete
  43. Thanks!! really great and easy to follow and understand example! really helped in my android uni proj!

    ReplyDelete
  44. Prasanna and other folks who benefited out of this,
    Can you please share the source code as an android project.

    Thanks in advance.

    ReplyDelete
  45. Great job Prasanta !!! Many many thanks !!!

    ReplyDelete
  46. Great job! But I only change

    public static final Parcelable.Creator CREATOR = new Creator() {
    public ParcelData createFromParcel(Parcel source) {
    return new ParcelData(source);
    }
    public ParcelData[] newArray(int size) {
    return new ParcelData[size];
    }
    };

    ReplyDelete
  47. Nice tutorial but why there is no real data placed in to Parcel? How to set data id, name... for each of element in Parcel list?

    ReplyDelete
  48. Nice tutorial! Thank you very much!

    ReplyDelete
  49. Good post... very helpful for beginners like me.
    As already said in one of the replies, I had to change the CREATOR to parcel it successfully.

    ReplyDelete
  50. What about circular references ?I think parcelable does not support circular references

    ReplyDelete
  51. Thanks for the helpful tutorial.

    ReplyDelete