Parcelable where/when is describeContents() used?

AndroidSerializationParcelableParcel

Android Problem Overview


Does anyone know where/when this method of a Parcelable is called?

@Override
public int describeContents() {
    return 0;
}

It has to be overriden. But should I consider doing something useful with it?

Android Solutions


Solution 1 - Android

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 you need to put FileDescriptor object into Parcelable you should/must specify CONTENTS_FILE_DESCRIPTOR as return value of describeContents(), i.e. by "special object" (in describeContents()'s description) they really mean: FileDescriptor.

This whole Parcelable functionality looks unfinished (read: has bad design). There is one other strange thing in the docs: >Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface

Implementing multiple inheritance by rules defined in human readable form? :-)

It seems like C++ programmer designed Parceable and at some point he realized: Oh, damn, there is no multiple inheritance in Java... :-)

Solution 2 - Android

There is only two possible value, 0 or CONTENTS_FILE_DESCRIPTOR

if you are serializing POLO, this value should always be 0, the CONTENTS_FILE_DESCRIPTOR is reserved for ParcelFileDescriptor, which could serialize a File Descriptor(FD) in *unix system.

Solution 3 - Android

From android framework, the only usage occurs in ActivityManagerService.java:

//ActivityManagerService.java
public int startActivityIntentSender(IApplicationThread caller,
    IntentSender intent, Intent fillInIntent, String resolvedType,
    IBinder resultTo, String resultWho, int requestCode,
    int flagsMask, int flagsValues) {
   // Refuse possible leaked file descriptors
   if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
       throw new IllegalArgumentException("File descriptors passed in Intent");
   }
   //...
}

Intent.java hasFileDescriptors() comes from Bundle.java hasFileDescriptors(). And the bundle will iterator all data in mMap(hashMap) or mParcelledData(Parcel). We will figure out intent.hasFileDescriptors() just wraps Parcel/Parcelable describeContents().

While, maybe this is the only usage for describeContents() : it use to filter FileDescriptor from Intent pass...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncodyView Question on Stackoverflow
Solution 1 - AndroidOgnyanView Answer on Stackoverflow
Solution 2 - Androidbowman hanView Answer on Stackoverflow
Solution 3 - AndroidallanView Answer on Stackoverflow