Skip to content

Instantly share code, notes, and snippets.

@LongClipeus
Created August 3, 2021 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LongClipeus/c39d648b8d753a3b97b8356eeebdc77d to your computer and use it in GitHub Desktop.
Save LongClipeus/c39d648b8d753a3b97b8356eeebdc77d to your computer and use it in GitHub Desktop.
How to make enum Parcelable in Kotlin?
import android.os.Parcel
import android.os.Parcelable
enum class TrafficLightStatus(val id: Int) : Parcelable {
RED(1),
YELLOW(2),
GREEN(3);
constructor(parcel: Parcel) : this(parcel.readInt())
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeInt(id)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<TrafficLightStatus> {
override fun createFromParcel(parcel: Parcel): TrafficLightStatus {
return values()[parcel.readInt()]
}
override fun newArray(size: Int): Array<TrafficLightStatus?> {
return arrayOfNulls(size)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment