Android Activity Results API unresolved reference in AppCompatActivity
Asked Answered
A

1

14

I'm trying to use the new Activity Results API to replace startActivityForResult. If I call registerForActivityResult in a Fragment everything is OK. However if I call the same method in an AppCompatActivity the IDE displays an "unresolved reference" error. Anyway the app builds with no errors an runs as expected. How to remove that "unresolved reference" error in the IDE?

I use: import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity

dependencies: "androidx.activity:activity-ktx:${versions.activity_ktx}" "androidx.fragment:fragment-ktx:${versions.fragment_ktx}"

Aftmost answered 2/7, 2020 at 16:41 Comment(8)
Which versions are you using for both KTX ?Plott
Are you using latest version like implementation androidx.activity:activity-ktx:1.2.0-alpha06Enallage
I'm using '1.2.0-alpha06' (activity-ktx) and '1.3.0-alpha06' (fragment-ktx). It's a multi-module project.Aftmost
I also have appcompat '1.3.0-alpha01'Aftmost
If I replace AppCompatActivity with ComponentActivity the IDE recognizes the method. However I can't use ComponentActivity.Aftmost
I fixed the bug casting the AppCompatActivity to ComponentActivity. However I don't know if this is correct: (this as ComponentActivity).registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { _ -> finish() }Aftmost
and the IDE now says "this cast can never succeed"Aftmost
It's an IDE issue; JetBrains has a ticket to track it. youtrack.jetbrains.com/issue/KTIJ-22124Indeciduous
B
30

Just struggled with this also for hours, I am using Android Studio 4.1 Beta. I think this is an IDE bug, since Gradle can build.

You can bypass this with casting to ComponentActivity at the moment:

private val requestPermission = (this as ComponentActivity).registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->

}

Hopefully this is solved in the next IDE releases.

Briant answered 23/7, 2020 at 13:43 Comment(1)
'this' is not defined in this context. I am using registerForActivityResult as a launcher.private val launcher = (this as ComponentActivity).registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result -> if (result.resultCode == Activity.RESULT_OK){ val task = GoogleSignIn.getSignedInAccountFromIntent(result.data) handleSignInResult(task) }Acuna

© 2022 - 2024 — McMap. All rights reserved.