Pass parameters in () -> Unit

Hello. I have an unfinished mechanism to override a method of an another class instance. Here is a part of it:

private var onActivityResultAction: ((requestCode: Int, resultCode: Int, data: Intent?) -> Unit)? = null

override fun doOnActivityResult(action: (requestCode: Int, resultCode: Int, data: Intent) -> Unit) {
    onActivityResultAction = action() //Error
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    onActivityResultAction?.invoke(requestCode, resultCode, data)
    super.onActivityResult(requestCode, resultCode, data)
}

How should I fix an error line ( onActivityResultAction = action()) if I have this?

Type mismatch.
Required: ((Int, Int, Intent?) β†’ Unit)?
Found: Unit

I believe you want to store the lambda you get passed, then it’s
onActivityResultAction = action

Note that your var has a data: Intent? as 3rd parameter, though action’s 3rd parameter is data: Intent. Those should be the same.

1 Like

Yeah, thanks. Found, I missed ? somewhere with Intent

1 Like