Ahmed Wagdi
Ahmed Wagdi

Reputation: 4371

Override OnActivityResult inside Intent is not working - kotlin

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.

Upvotes: 4

Views: 4091

Answers (1)

guipivoto
guipivoto

Reputation: 18677

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
    ...
}

Upvotes: 8

Related Questions