17

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'
Krishna Meena
  • 5,693
  • 5
  • 32
  • 44
  • Have a look at the dependencies listed on your linked page. It doesn't explicitly state so, and I've not checked yet, but those are probably the minimum versions necessary. – Mike M. Jul 24 '20 at 04:35
  • Yes I tried with those mentioned versions also but same error then I thought to use stable versions. – Krishna Meena Jul 24 '20 at 04:38
  • 1
    Yeah, I got the same behavior. Odd. However, my IDE tells me the newest versions are `alpha07`, and it works as expected when upgrading to those. That is, `"androidx.activity:activity:1.2.0-alpha07"` and `"androidx.fragment:fragment:1.3.0-alpha07"`. – Mike M. Jul 24 '20 at 04:44

4 Answers4

21

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

Troy
  • 690
  • 1
  • 7
  • 25
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 5
    I have the same issue and having this didn't help. The IDE shows "Unresolved reference: registerForActivityResult" , yet it can build just fine... How could it be? – android developer Jun 28 '21 at 11:31
  • Also unable to get Android Studio to autocomplete `registerForActivityResult`. It's saying "Unresolved reference: registerForActivityResult" so I cannot code with this api at all. – Troy Aug 25 '21 at 07:21
4

try this one new version is come.

 implementation 'androidx.activity:activity-ktx:1.3.1'
Najib.Nj
  • 3,706
  • 1
  • 25
  • 39
1

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.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
LearningCharlito
  • 327
  • 1
  • 2
  • 20
0

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.

Gabriel TheCode
  • 163
  • 2
  • 8