Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity Results API unresolved reference in AppCompatActivity

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

like image 738
iClaude Avatar asked Jul 02 '20 16:07

iClaude


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

like image 191
Minki Avatar answered Oct 12 '22 04:10

Minki