How to format getSerializableExtra() arguments for data class object

I was following Philip Lackner’s course on passing data from one activity to another using a Serializable data class (g4dHead). The original functions used by Lackner are now deprecated, however I have succeeded in coding the forward transaction as follows:

val gatorHd = g4dHead(setCurrentMode,motorIsRunning,btPaired,btBound,
btAddress, motorSpeed, ledDuration, ledTiming, bLedIsActive)

    val jsonString_g4d = Json.encodeToString(gatorHd)

    binding.ibtnBluetooth.setOnClickListener{
        Intent(this, btActivity::class.java ).also {
            it.putExtra("EXTRA_G4D", jsonString_g4d)
            startActivity(it)
        }
    } 

Within the target activity (btActivity) I don’t know how to format the getSerializableExtra arguments to satisfy the compiler which advises I have a type mismatch for the second parameter that must be of type Serializable. The hint to correct the problem is to cast the second parameter expression to type Serializable. At this point I’m stuck, nothing I have tried has worked; I’m a novice with Kotlin, only a month of self teaching so would appreciate any help from forum members :wink:

val myIntent = intent
val derivedObject = myIntent.getSerializableExtra(“EXTRA_G4D”, g4dHead::class.java)

getSerializableExtra() gets an object that uses Java’s serialization, not Kotlin’s. You have added a String as an extra, so use getStringExtra() and deserialize it yourself.

1 Like