Is it possible to create a HashMap that is Parcelable on Android?

不羁岁月 提交于 2019-11-30 08:30:18

The way your class ContentItemContainer is implemented - as extending HashMap, your writeToParcel and Parcelable.Creator will never be called.

The reason is that Map is a valid data type to be put in a Parcel, so that the class gets flattened using the logic of the HashMap class, and not yours. This is because the way Parcel is implemented, it checks whether the supplied values are descendants of certain classes in a specific order.

When unparcelling the extras, a HashMap will be created from your object's data consequently.

To work around this, you could hide the HashMap inside your class and expose getters/putters for the HashMap. This is exactly the same way that ContentValues is implemented, and parcelling/unparcelling it works without any problems.

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