Android Activity Results API unresolved reference in AppCompatActivity

Viewed 4486

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

2 Answers

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.

add this dependency in your build.gradle

 def activity_version = "1.2.1"
 def fragment_version = "1.3.1"
 implementation "androidx.activity:activity-ktx:$activity_version"
 implementation "androidx.fragment:fragment-ktx:$fragment_version"
Related