How do I create template interface implementations for Android

Viewed 27

If you use Android Studio as an IDE, and create a class implementing the Parcelable interface, the linter will tell you that you have not implemented two of the required members. One of the contextual actions is to implement the methods, which is standard. However, there is another action to "Add Parcelable implementation". When you click it, it adds a bunch of boilerplate for you.

For example, I go from this class

class Class(val name: String)

To this

class Class(val name: String) : Parcelable {
    constructor(parcel: Parcel) : this(parcel.readString()) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Class> {
        override fun createFromParcel(parcel: Parcel): Class {
            return Class(parcel)
        }

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

I would like to create an interface which has a similar boilerplate generator. How would I go about doing this?

0 Answers
Related