I'm aware of the performance differences between Parcelable
(fast) and Serializable
(slow). However, I need to store certain application information persistently, not just within one lifecycle, thus onSaveInstanceState and associated methods utilising Parcelable objects aren't appropriate.
So I turned my attention to Serializable
. Primarily I have AbstractList
types to store - which is fine, since they implement Serializable
. However, many of the types I store inside these are Parcelable
but not Serializable
, e.g. RectF
.
I thought "no problem", since I can easily generate a Parcel via Parcelable.writeToParcel(parcel, flags)
then call marshall()
on it to create a byte[]
which I can serialize and deserialize. I figured I'd use generics; create a SerializableParcelable<Parcelable> implements Serializable
class, allowing a one-fit solution for all Parcelable
types I wish to serialize. Then I would e.g. store each RectF inside this wrapper within ArrayList
, and lo-and-behold the list and its Parcelable
contents are serializable.
However, the API docs state that marshall()
mustn't be used for persistent storage:
public final byte[] marshall ()
Returns the raw bytes of the parcel.
The data you retrieve here must not be placed in any kind of persistent storage (on local disk, across a network, etc). For that, you should use standard serialization or another kind of general serialization mechanism. The Parcel marshalled representation is highly optimized for local IPC, and as such does not attempt to maintain compatibility with data created in different versions of the platform.
So now I'm stuck. I can either ignore this warning and follow the route I've outlined above, or else circumvent the issue by extending each individual Parcelable
I want to serialize and creating bespoke serialization methods, which seems extremely wasteful of time and effort.
Does anyone know of a 'correct' shortcut to serialize a Parcelable
object without using marshall()
? Or should I plough on without heeding the warning specified? Perhaps an SQLite database is the way to go, but I'm unsure and would like your advice.
Many thanks.
SharedPreferences
to store the persistant data? Oh, wait, the answer to that is obvious, sorry! – Caterpillar