36

According to this documentation from Google about launching an activity to get a result:

While the underlying startActivityForResult() and onActivityResult() APIs are available on the Activity class on all API levels, it is strongly recommended to use the Activity Result APIs introduced in AndroidX Activity 1.2.0-alpha02 and Fragment 1.3.0-alpha02.

I want to let my users take a photo from within my app and get that photo data back to my app. I was going to use the old startActivityForResult() but this new method looks like it will solve lots of problems and be more reliable, so I wanted to give it a try. I'm supposed to be able to call registerForActivityResult() and pass it a built-in contract for taking a picture called ActivityResultsContracts.TakePicture:

this.registerForActivityResult(new ActivityResultContracts.TakePicture(), ...);

But I get: error: package ActivityResultContracts does not exist

I've added this to my app/build.gradle:

// original include
//implementation 'androidx.appcompat:appcompat:1.1.0'

// suggestion from Google documentation
//implementation 'androidx.appcompat:appcompat:1.2.0-alpha02'

// AndroidStudio suggested a newer version was available
implementation 'androidx.appcompat:appcompat:1.2.0-beta01'

I tried the alpha02 and the beta01 and neither of them seem to have the classes referred to in the documentation.

When I try to import the class manually at the top of my java file, AndroidStudio doesn't think the package exists either. It should be androidx.activity.result.contract.ActivityResultContracts.TakePicture, but this is what I see:

screenshot of Android Studio auto-complete package list in import statement

I'm using gradle 3.5.3 if that matters at all. (Whenever I try to upgrade to the latest gradle, my project goes insane, so I've just been staying with the version that works.)

Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229

3 Answers3

67

From the quoted documentation:

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

Neither of those are in your dependencies, at least the portion from your question. You have been manipulating appcompat, not activity or fragment.

Add either or both of:

implementation "androidx.activity:activity:1.2.0"
implementation "androidx.fragment:fragment:1.3.0"

(or any higher versions)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Ohhhh.... /facepalm. Thank you. I saw `androidx` in my implementation line for the appcompat and just updated the version number without even noticing it was specifically just appcompat and not the activity package. Thanks again! – Kenny Wyland May 05 '20 at 21:51
  • 2
    I had a similar issue (unresolved method `registerForActivityResult`) even if I did have the two dependencies listed in the Gradle file. However they were for alpha version 02, as per Google documentation. The problem was solved when I changed them to 04 as per [CommonsWare](https://stackoverflow.com/users/115145/commonsware) recommendation. Thanks a lot! – Franco May 30 '20 at 19:06
  • For some reason I added these (and their newer versions) to some old project and it couldn't find the function, while it works fine on a POC. How could it be? This is what I added: ` implementation "androidx.fragment:fragment-ktx:1.3.0-rc01" implementation "androidx.fragment:fragment:1.3.0-rc01" implementation 'androidx.activity:activity:1.2.0-rc01' implementation 'androidx.activity:activity-ktx:1.2.0-rc01'` – android developer Jan 25 '21 at 15:04
  • @androiddeveloper: You do not need the duplicates -- the `-ktx` artifacts should be pulling in the non-`-ktx` artifacts. However, I do not know why you are running into problems. For example, [here is `ActivityResultsContracts`](https://androidx.tech/artifacts/activity/activity/1.2.0-rc01-source/androidx/activity/result/contract/ActivityResultContracts.java.html) for `androidx.activity:activity:1.2.0-rc01`. – CommonsWare Jan 25 '21 at 15:21
  • @CommonsWare Using just `androidx.activity:activity:1.2.0-rc01` also didn't help. Only similar function it allows me to use is `registerStartActivityForResultReceiver`, and it's very different... In POC it works fine (I used `implementation "androidx.fragment:fragment-ktx:1.3.0-rc01"` there). Is it possible it's because of a warning of `Plugin version (1.4.21) is not the same as library version (1.3.72) ` (even though I've set it to 1.4.21 everywhere) ? – android developer Jan 25 '21 at 15:28
  • 3
    @CommonsWare I think it's a bug of the IDE or its plugins. It seems the IDE can't help with it (meaning it's mostly a visual issue), but building, creating APK and launching - all works fine... Wrote about this here: https://issuetracker.google.com/issues/178403178 and here : https://www.reddit.com/r/android_devs/comments/l4qmo5/cant_use_registerforactivityresult_even_though_i/gkt2srk/?context=3 – android developer Jan 26 '21 at 10:07
  • 1
    @androiddeveloper: I have run into that sort of problem before with Studio, where the IDE yells but the project builds just fine. I do not recall encountering a case where a function was missing, but I have definitely seen cases where Studio claimed that classes do not exist when they did. `¯\_(ツ)_/¯` – CommonsWare Jan 26 '21 at 12:34
  • @CommonsWare I had this sometimes with view-binding, but somehow it got solved on its own (or I used invalidate-cache for this). Here no matter what I try it doesn't help. I hate seeing red-underlines :) – android developer Jan 26 '21 at 13:44
  • you sir deserve true glory, your personal space in heaven and 72 virgins granted twice... Thanks for saving my day – LuckyLikey Mar 24 '21 at 19:34
  • Damn this was all about the imports. This is so complicated by Google! Everything works fine without this import. – sud007 Apr 21 '22 at 05:22
  • Added both - still have the issue. Actually, added the higher versions `1.4.0` but I'm still seeing this error. – JCutting8 Jun 06 '22 at 11:43
  • Please refer - https://stackoverflow.com/questions/70919493/function-registerforactivityresult-not-exist too – arango_86 Jul 24 '22 at 12:55
  • I see the same thing as @androiddeveloper, the IDE isn't happy but the project builds and runs. I swear in the last 6 months the IDE has gotten increasingly buggy. – johngray1965 May 08 '23 at 15:28
20

I was having the same problem. When I upgraded androidx.appcompat from version 1.2.0 to 1.3.1, the problem went away. There's no need to add androidx.activity or androidx.fragment dependencies.

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.1'
    ...
}
tronman
  • 9,862
  • 10
  • 46
  • 61
0

I had the same problem but instead of adding something I erased the appcompat implementation that probably caused the problem because after it everything worked just fine.

AGJ
  • 23
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 16 '23 at 00:06