Android ActivityResult API unresolved reference error registerForActivityResult

18k Views Asked by At

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'
4

There are 4 best solutions below

2
On BEST ANSWER

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

0
On

Make sure you added these dependencies in your build.gradle

implementation 'androidx.activity:activity-ktx:1.7.2'
implementation 'androidx.fragment:fragment-ktx:1.6.0'

If it doesn't works then clear your file system cache and local history with File -> Invalidate Cache & Restart and rebuild your project.

3
On

try this one new version is come.

 implementation 'androidx.activity:activity-ktx:1.3.1'
0
On

Just in case someone else has this problem when working in an old project...

I saw in a different computer using inheritance that your activity extends from AppCompatActivity() which extends from FragmenteActivity().

I notice FragmentActivity is different from the one were I was having this issue...

So I updated the appcompat library, using :

 androidx.appcompat:appcompat:1.4.1

And this solved the issue.