Created
August 3, 2021 15:45
-
-
Save LongClipeus/c39d648b8d753a3b97b8356eeebdc77d to your computer and use it in GitHub Desktop.
How to make enum Parcelable in Kotlin?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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