Parcelable where/when is describeContents() used?

There is a constant defined in Parcelable called CONTENTS_FILE_DESCRIPTOR which is meant to be used in describeContents() to create bitmask return value. Description for CONTENTS_FILE_DESCRIPTOR in the API ref is: Bit masks for use with describeContents(): each bit represents a kind of object considered to have potential special significance when marshalled. Which really means: If … Read more

How to use Parcel in Android?

Ah, I finally found the problem. There were two in fact. CREATOR must be public, not protected. But more importantly, You must call setDataPosition(0) after unmarshalling your data. Here is the revised, working code: public void testFoo() { final Foo orig = new Foo(“blah blah”); final Parcel p1 = Parcel.obtain(); final Parcel p2 = Parcel.obtain(); … Read more

Android E/Parcel﹕ Class not found when unmarshalling (only on Samsung Tab3)

For some strange reason it looks like the class loader isn’t set up properly. Try one of the following in TestActivity.onCreate(): TestParcel cfgOptions = getIntent().getParcelableExtra(“cfgOptions”); Intent intent = getIntent(); intent.setExtrasClassLoader(TestParcel.class.getClassLoader()); TestParcel cfgOptions = intent.getParcelableExtra(“cfgOptions”); Bundle extras = getIntent().getExtras(); extras.setClassLoader(TestParcel.class.getClassLoader()); TestParcel cfgOptions = extras.getParcelable(“cfgOptions”); Alternatively, wrap the parcelable into a bundle: Bundle b = new Bundle(); … Read more

How to marshall and unmarshall a Parcelable to a byte array with help of Parcel?

First create a helper class ParcelableUtil.java: public class ParcelableUtil { public static byte[] marshall(Parcelable parceable) { Parcel parcel = Parcel.obtain(); parceable.writeToParcel(parcel, 0); byte[] bytes = parcel.marshall(); parcel.recycle(); return bytes; } public static Parcel unmarshall(byte[] bytes) { Parcel parcel = Parcel.obtain(); parcel.unmarshall(bytes, 0, bytes.length); parcel.setDataPosition(0); // This is extremely important! return parcel; } public static <T> … Read more