41

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

5 Answers 5

69

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)

13
  • 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! Commented May 5, 2020 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 recommendation. Thanks a lot!
    – Franco
    Commented May 30, 2020 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'` Commented Jan 25, 2021 at 15:04
  • 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: issuetracker.google.com/issues/178403178 and here : reddit.com/r/android_devs/comments/l4qmo5/… Commented Jan 26, 2021 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. ¯\_(ツ)_/¯ Commented Jan 26, 2021 at 12:34
22

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'
    ...
}
2
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.

1
  • 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.
    – Community Bot
    Commented May 16, 2023 at 0:06
0

I got the same issue and neither option work for me I import "import androidx.activity.result.contract.ActivityResultContracts;" this in my activity and it works.

I am using "'androidx.appcompat:appcompat:1.6.1'" library

1
  • implementation 'androidx.appcompat:appcompat:1.5.0' use it. its works for me
    – Gayathri
    Commented Sep 22, 2023 at 15:01
0

If anyone is still facing this issue and tried all the things, check the modules that you have added in your project, it's very much possible that one of the module has implemented older version of androidx.appcompat:appcompat

In my case, I had added 'nativetemplates' module for Admob native ads, which had older version of androidx.appcompat:appcompat:1.2.0, I simply updated it to latest version and it resolved the issue.

Not the answer you're looking for? Browse other questions tagged or ask your own question.