Persisting A Parcelable Object In Android
I have a class in my Android app that I've made Parcelable so that it can be passed between Activities. I would like to be able to save this object to the filesystem. It seems that
Solution 1:
From http://developer.android.com/reference/android/os/Parcel.html
Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.
Solution 2:
For this problem, I did the following:
- Implemented Serializable in my object
- Added a toJSON() method to convert the object to a JSON object
- Used a custom JSONSerializer to write the JSON objects to a file
- Added a constructor that takes a JSON object as a parameter, used by the custom JSONSerializer
It ended up being pretty simple...I can paste some sample code if needed.
Post a Comment for "Persisting A Parcelable Object In Android"