androidandroid-studiokotlinregisterforactivityresultgetcontent

Android ActivityResult API unresolved reference error registerForActivityResult


I am trying to use ActivityResult APIs as stated here ActivityResult APIs

But it is giving unresolved reference error.

enter image description here

I am using following dependencies

api 'androidx.activity:activity:1.1.0'
api 'androidx.activity:activity-ktx:1.1.0'
api 'androidx.fragment:fragment:1.2.5'
api 'androidx.fragment:fragment-ktx:1.2.5'

Solution

  • As per the Getting a result from an activity guide:

    it is strongly recommended to use the Activity Result APIs introduced in AndroidX Activity 1.2.0-alpha02 and Fragment 1.3.0-alpha02.

    So using the stable versions will not include those APIs. The latest versions of each is alpha07, which would mean your dependencies would be:

    api 'androidx.activity:activity-ktx:1.2.0-alpha07'
    api 'androidx.fragment:fragment-ktx:1.3.0-alpha07'
    

    NOTE: your activity must extend from ComponentActivity, which was introduced in AppCompat 1.1.0 and includes the registerForActivityResult() API