Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sealed class hierarchy Parcelable

I have something similar like the following where I want to pass them around as intent arguments;

sealed class BasketType : Parcelable {

    class BasketOne(val basketId: String): BasketType() {

        constructor(parcel: Parcel) : this(parcel.readString()) {
        }

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

        override fun describeContents(): Int {
            return 0
        }

        ...
    }

    ...
}

But I get the following error;

Abstract member cannot be accessed directly

on the line super.writeToParcel(parcel, flags) which is kind of expected.

I've looked around for a workaround but could not find one. Any ideas?

like image 308
stdout Avatar asked Sep 05 '19 17:09

stdout


People also ask

Can sealed class be Parcelable?

sealed and abstract classes can also be parcelized. For this, the parent class needs to implement Parcelable and all the child classes need to be annotated with @Parcelize .

What is Parcelable class in Java?

Parcelable is a serialization mechanism provided by Android to pass complex data from one activity to another activity.In order to write an object to a Parcel, that object should implement the interface “Parcelable“.

What is a primary purpose of the Parcelable interface?

The Parcelable interface adds methods to all classes you want to be able to transfer between activities. These methods are how parcelable deconstructs the object in one activity and reconstructs it in another. For this example you'll look at how to implement parcelable in a simple class.

Is data class Parcelable Kotlin?

In Android, to pass the data from one activity to another activity, we use the Parcelable. The kotlin-parcelize plugin provides a Parcelable implementation generator. The Android's Parcelable is an interface on which, the values can be written and read from a Parcel.


2 Answers

if you want @Parcelize in sealed class, do it like this:

sealed class AssistantType : Parcelable{
    @Parcelize data class Dashboard(
        val firstName: String,
        val hasGoal: Boolean,
        val hasOverDuePayment: Boolean
    ) : AssistantType()

    @Parcelize data class Detail(
        val firstName: String,
        val isGoalAchived: Boolean
    ) : AssistantType()
}

and Add below code into your App Level gradle.

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

androidExtensions {
    experimental = true
}
like image 165
Adnan Abdollah Zaki Avatar answered Sep 22 '22 20:09

Adnan Abdollah Zaki


You simply leave out the super call. There is no super implementation of that function, which is why it's complaining.

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

the super call is only there because the code generation of android studio puts it there without checking that it's actually possible.

like image 39
zapl Avatar answered Sep 23 '22 20:09

zapl