Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin getParcelableArray from intent bundle not able to cast it to custom type

I am running into a weird problem . I have a class A which implements Parcelable interface in kotlin.

I am passing the array of class A from one activity to another no issues here.

 var arrayOfA:Array<A> // just to tell the type assume the array is initialised with the value
 intent.putExtra("array", arrayOfA)

But while receiving it in another activity , I am not able to assign it to variable of type Array it is asking me to assign it to Array when A is on type parcelable why I am not able to assign it the variable.

in second Activity 
var arrayOfA:Array<A>?=null


arrayA=intent.get("array") as Array<A> // Problem here. Class cast exception

I am not able to understand why. Can some one help me here. I don't want to change the type of variable to Array as it as many inter depedencies.(The class here is just a demonstration)

========================================

class A(val a:String?,val b:String?):Parcelable {
    constructor(parcel: Parcel) : this(
            parcel.readString(),
            parcel.readString()) {
    }

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

    override fun describeContents(): Int {
        return 0
    }

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

        override fun newArray(size: Int): Array<A?> {
            return arrayOfNulls(size)
        }
    }
}
like image 265
Nitin Mesta Avatar asked Sep 12 '17 05:09

Nitin Mesta


3 Answers

I ran into the same problem. This is how I was able to workaround it:

arrayOfA = intent.getParcelableArrayExtra("array").map { it as A }.toTypedArray()

For some reason this does not work:

arrayOfA = intent.getParcelableArrayExtra("array") as Array<A>

That unfortunately exhibits a warning

Unchecked cast: Array<(out) Parcelable!>! to Array)

and runtime exception

java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.company.app.A[]

like image 124
Jordan H Avatar answered Nov 07 '22 01:11

Jordan H


Bundle.getParcelableArray (and Intent.getParcelableArrayExtra) return Parcelable[]. You can't cast Parcelable[] as MyClass[]. You can cast an element of Parcelable[] as MyClass.

Use Bundle.getParcelableArrayList (or its Intent counterpart) instead, because it has a generic type.

var values: List<MyClass> = listOf(...)
intent.putParcelableArrayListExtra("key", values.asArrayList())
values = intent.getParcelableArrayListExtra("key")!!

fun <E> List<E>.asArrayList(): ArrayList<E> = this as? ArrayList<E> ?: ArrayList(this)
like image 45
Eugen Pechanec Avatar answered Nov 07 '22 02:11

Eugen Pechanec


You need to use getParcelableArrayExtra to retrieve Parcelable objects as in

arrayA = intent.getParcelableArrayExtra("array") as Array<A?>

Make class A as Nullable all over, because you can't cast Nullable to Non Nullable in Kotlin

creator should be like below

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

        override fun newArray(size: Int): Array<A?> {
            return arrayOfNulls(size)
        }
    }
like image 2
Rajan Kali Avatar answered Nov 07 '22 02:11

Rajan Kali