Bulk Emailing

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 16 June 2010

Android Parcelable Example

Posted on 00:17 by Unknown

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? :-)

Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Weekend at Yeouido (Han River Cruise)
    Yeouido is one of the Han River Cruise boarding points. It has a wonderful river side park (Yeouido P...
  • CityWeather
    Update: Release 1.1 has been uploaded. It will now provide weekly forecast of your selected cities. Download   CityWeather is an Android...
  • Few Multi-Threading Implementations
    Sequential Producer & Consumer In my first example, I will demonstrate an implementation to solve ...
  • Accessing Yahoo Finance API
    Since last few days I was wondering the right set of Web Service to read Country wise Stock Exchange index information . I found a bunch of ...
  • Regular Expression in JavaScript
    I was struggling to get familiar with JavaScript's Regular expression handling and thought my learning might help others. Here I'll...
  • Alphanumeric Random Number
    Sometime basic things becomes tricky :-) Was trying to find out ways to generate some alphanumeric random number whose size (i.e. number of ...
  • Binary Search Tree in Java
    This post is a part of my continuous effort to just implement few of the important data structures in ...
  • QuickSort in Java
    This post is a part of my continuous effort to just implement few of the important data structures in Java. Please find my previous post on ...
  • InDroid- Access to Android's Hidden API
    I have started one project " InDroid " to expose and share Hidden and Internal methods and variables of Android platform which are...
  • Image Geo Tagging for Android
    " onMap " is an Image Geo Tagging application, which can read EXIF header of images stored in your Android device and Map them ov...

Blog Archive

  • ►  2013 (6)
    • ►  September (2)
    • ►  May (1)
    • ►  April (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (4)
    • ►  July (2)
    • ►  March (1)
    • ►  January (1)
  • ►  2011 (11)
    • ►  November (1)
    • ►  October (2)
    • ►  August (1)
    • ►  June (1)
    • ►  April (2)
    • ►  March (3)
    • ►  January (1)
  • ▼  2010 (27)
    • ►  December (2)
    • ►  November (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (4)
    • ▼  June (7)
      • HashMap Internal
      • Binary Search Tree in Java
      • QuickSort in Java
      • Android Parcelable Example
      • CityWeather
      • LinkedList in Java
      • Android- Key Notes
    • ►  May (5)
Powered by Blogger.

About Me

Unknown
View my complete profile