Skip to content

Instantly share code, notes, and snippets.

@rlac
Created January 15, 2015 23:08
Show Gist options
  • Save rlac/69b5f6eeb55c3d36d0bc to your computer and use it in GitHub Desktop.
Save rlac/69b5f6eeb55c3d36d0bc to your computer and use it in GitHub Desktop.
Parcelable creator convenience function
import android.os.Parcelable
import android.os.Parcel
import kotlin.InlineOption.ONLY_LOCAL_RETURN
public inline fun creator<reified T : Parcelable>(
inlineOptions(ONLY_LOCAL_RETURN) createFromParcel: (Parcel) -> T?): Parcelable.Creator<T> =
object : Parcelable.Creator<T> {
override fun createFromParcel(source: Parcel): T? = createFromParcel(source)
override fun newArray(size: Int): Array<out T?> = arrayOfNulls(size)
}
import android.os.Parcelable
import android.os.Parcel
import kotlin.platform.platformStatic
class Test(var int: Int, var string: String) : Parcelable {
override fun describeContents(): Int = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeInt(int)
dest.writeString(string)
}
class object {
platformStatic val CREATOR = creator { p -> Test(p.readInt(), p.readString()) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment