Override OnActivityResult inside Intent is not working - kotlin

Viewed 3624

I'm overriding my OnActivityResult to control some flow in the application.

The problem that it gives an error that it is overriding nothing.

here is the code am using :

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
}

The error only gone when I add nonnull protection after Intent, to have the code like this :

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
}

But that causes a kotlin.KotlinNullPointerException error after in the function itself.

1 Answers

Signature of method onActivityResult is:

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)

As you can see, Intent data is nullable. So, in order to override the method properly, you must use:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)

Since data is nullable, you must use the safe call operator ? everytime you need to check the intent.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    val action = data?.action
    ...
}
Related