0

I have class Data with some properties, where one of the property is a class which implements Serializable and another one implements Parcelable, other properties is String.

And I want to make this Class (Data) implement Serializable.

Is there any problems during serialization of Data class with property which implements Parcelable, but not implements Serializable?

researcher
  • 1,758
  • 22
  • 25
  • If you ever need to serialize a `Parcelable`, you may be able to do this by converting the `Parcelable` property to bytes first (more on this [here](https://stackoverflow.com/questions/18000093/how-to-marshall-and-unmarshall-a-parcelable-to-a-byte-array-with-help-of-parcel)) in `writeObject` of the `Data` class, while doing the opposite operation in `readObject`. Another option according to [the documentation on `Serializable`](https://developer.android.com/reference/java/io/Serializable) seems to be to have a no-arg constructor for the `Parcelable` property. – gthanop Jul 14 '21 at 18:42

1 Answers1

1

There are no issues as you don't introduce any new properties for the Parcelable implementation. Standard implementation merely provides you some helper methods describeContents and writeToParcel and a static CREATOR object, which will not be part of the Serialized content.

example:

 class MyParcelable private constructor(`in`: Parcel) : Parcelable {
     private val mData: Int = `in`.readInt()

     override fun describeContents(): Int {
         return 0
     }

     override fun writeToParcel(out: Parcel, flags: Int) {
         out.writeInt(mData)
     }

     companion object {
         val CREATOR: Parcelable.Creator<MyParcelable?>
                 = object : Parcelable.Creator<MyParcelable?> {
             override fun createFromParcel(`in`: Parcel): MyParcelable? {
                 return MyParcelable(`in`)
             }

             override fun newArray(size: Int): Array<MyParcelable?> {
                 return arrayOfNulls(size)
             }
         }
     }
 }

Cristian Holdunu
  • 1,880
  • 1
  • 18
  • 43