Created
March 9, 2017 18:59
-
-
Save DevPicon/ade51aa589caee02fcfb244b76fe7cc9 to your computer and use it in GitHub Desktop.
This gist shows you how to implement Parcelable on Kotlin classes
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 | |
/** | |
* Created by armando on 3/9/17. | |
*/ | |
data class Event(val id: Int, val name: String, val type: String, val venue: Venue? = null ) : Parcelable { | |
companion object { | |
@JvmField val CREATOR: Parcelable.Creator<Event> = object : Parcelable.Creator<Event>{ | |
override fun newArray(size: Int): Array<Event?> = arrayOfNulls<Event>(size) | |
override fun createFromParcel(source: Parcel): Event = Event(source) | |
} | |
} | |
constructor(source: Parcel):this( | |
source.readInt(), | |
source.readString(), | |
source.readString(), | |
source.readParcelable(Event::class.java.classLoader) | |
) | |
override fun writeToParcel(dest: Parcel?, flags: Int) { | |
dest?.let { | |
dest.writeInt(id) | |
dest.writeString(name) | |
dest.writeString(type) | |
dest.writeParcelable(venue, flags) | |
} | |
} | |
override fun describeContents(): Int = 0 | |
} | |
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 | |
/** | |
* Created by armando on 3/9/17. | |
*/ | |
data class Venue(val name: String, val address: String, var longitude: Int = 0, var latitude: | |
Int = 0) : Parcelable { | |
companion object { | |
@JvmField val CREATOR: Parcelable.Creator<Venue> = object : Parcelable.Creator<Venue> { | |
override fun newArray(size: Int): Array<Venue?> = arrayOfNulls(size) | |
override fun createFromParcel(source: Parcel): Venue = Venue(source) | |
} | |
} | |
constructor(source: Parcel) : this(source.readString(), source.readString(), source.readInt(), | |
source.readInt()) | |
override fun writeToParcel(dest: Parcel?, flags: Int) { | |
dest?.let { | |
dest.writeString(name) | |
dest.writeString(address) | |
dest.writeInt(longitude) | |
dest.writeInt(latitude) | |
} | |
} | |
override fun describeContents(): Int = 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment