title: Implementing Parcelable interface on polymorphic arrays
body: <p>I have an array of type named <code>ItinerarySegment</code>, and this type has subclasses: <code>WalkSegment</code>, <code>BusSegment</code> etc.</p>
  
  <pre><code>public interface ItinerarySegment
  {
  
  }
  
  public class WalkSegment implements ItinerarySegment
  {
  
  }
  
  public class BusSegment implements ItinerarySegment
  {
  
  }
  </code></pre>
  
  <p>What sort of strategy should I follow when making the array of <code>ItinerarySegment</code> parcelable? The main concern here is how it will be used later when re-constructing the array via the <a href="http://developer.android.com/reference/android/os/Parcel.html#createTypedArray%28android.os.Parcelable.Creator%3CT%3E%29" rel="nofollow">createTypedArray</a> method (prepared by <a href="http://developer.android.com/reference/android/os/Parcel.html#writeTypedArray%28T%5B%5D,%20int%29" rel="nofollow">writeTypedArray</a> method).</p>
  
  <p>In which the <a href="http://developer.android.com/reference/android/os/Parcel.html#createTypedArray%28android.os.Parcelable.Creator%3CT%3E%29" rel="nofollow">createTypedArray</a> method take a <a href="http://developer.android.com/reference/android/os/Parcelable.Creator.html" rel="nofollow">Creator</a> field parameter. The problem lies here...Where should the <a href="http://developer.android.com/reference/android/os/Parcelable.Creator.html" rel="nofollow">Creator</a> field be defined? (in <code>ItinerarySegment</code>, <code>WalkSegment</code>, or <code>BusSegment</code>?).</p>
  
  <pre><code>public static final Creator&lt;Typename&gt; CREATOR = new Parcelable.Creator&lt;Typename&gt;()
  {
      public Typename createFromParcel(Parcel in)
      {
          return new Typename(in);
      }
  
      public Typename[] newArray(int size)
      {
          return new Typename[size];
      }
  
  };
  </code></pre>
  
  <p>If I make <code>ItinerarySegment</code> an abstract class and define the <a href="http://developer.android.com/reference/android/os/Parcelable.Creator.html" rel="nofollow">Creator</a> field's method, then the subsequent subclasses's data will be lost since neither of their constructors are called with the Parcel parameter, instead <code>ItinerarySegment</code>'s constructor will be called.</p>
  
  <pre><code>constructor(Parcel in);
  </code></pre>
  
  <p>If I make <code>WalkSegment</code> define <a href="http://developer.android.com/reference/android/os/Parcelable.Creator.html" rel="nofollow">Creator</a> field, then <code>BusSegment</code> would have a problem.</p>
  
  <p>Any clarification needed?</p>