Parcelable where/when is describeContents() used?

☆樱花仙子☆ 提交于 2019-12-17 10:18:09

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4076946/parcelable-where-when-is-describecontents-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!