Skip to content Skip to sidebar Skip to footer

Android User Object Parcelable Issue

I am developing simple travel agent android application. When application starts it loads list of cities, in the next activity user will see source and destination spinners, once u

Solution 1:

My understanding is that you shouldn't use Parcels for this. From the android documentation on Parcels:

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.

What you should do is create two static methods for each class you want to transfer to the next activity. Call one of them toBundle() and one of them fromBundle(). The first converts a given instance of you class to a bundle while the second creates a new instance of your class from a bundle. Every time you need to pass an object on to the next Activity, just bundle it up using the toBundle() method and add it to the Intent you're using to start the next Activity. In the next Activity, you can recreate the object by calling fromBundle().

Post a Comment for "Android User Object Parcelable Issue"