Search code examples
kotlinandroid-fragmentslambdabundleserializable

How do you deserialize a serialized lambda function in kotlin?


I have a lambda expression:

val lambda: ((ArrayList<String>) -> Unit)

I put this into a Bundle as a Serializable, like so:

putSerializable(LAMBDA, lambda as Serializable)

How do I Deserialize the lambda back into the function type: "((ArrayList) -> Unit)" ?

I'm trying to get this to work:

            state.getSerializable(LAMBDA)?.also {
               val deserializedLambda: ((ArrayList<String>) -> Unit) = it
            }

"it" is Serializable. But I need to deserialize it somehow to get it back to being of type ((ArrayList) -> Unit), which is the type of the variable "changes".

Thanks for your help


Solution

  • Presumably you need a cast: it as (ArrayList) -> Unit.