Android: Parcelable.writeToParcel and Parcelable.Creator.createFromParcel are never called

混江龙づ霸主 提交于 2019-11-28 17:33:01

Apparently the Android Bundle class does not adhere to the parcelable protocol that instead is followed during IPC marshalling.

Instead, it seems like the Bundle implementation just writes and reads the Parcelable object into its own internal map by means of reflection. From a test we did, it seems that the Bundle writes/reads every field defined in your Parcelable-derived class, just because you have declared those fields.

Luis

Technically, the documentation doesn't say that writeToParcel or createFromParcel are called from onSaveInstance. As a matter of fact, if you check the savedState in your code you are going to find that it is exactly the same object instance in both the save and the restore cases; it makes sense to avoid serialize-deserialize if you can.
OTOH, the documentation doesn't say either that serialization is not done. The conclusion should be that you shouldn't depend on either case, just assume that you get the correct bundle.

Also, you may want to check http://developer.android.com/guide/topics/resources/runtime-changes.html

I confirm what superjos said. On the saveToBundle event, Android bundle just stores the class's members per reflection and doesn't call the Parcelable functions. I have lost one day on this problem! sad....

This is really bad .... This means you potentially stores a huge amount of data for nothing using this way.

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