Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidX ActivityResultContracts package not found / class not found

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

like image 496
Kenny Wyland Avatar asked May 05 '20 20:05

Kenny Wyland


People also ask

What is ActivityResultContracts?

An ActivityResultContract to prompt the user to pick one or more a pieces of content, receiving a content:// Uri for each piece of content that allows you to use android. content. ContentResolver.

What is ActivityResultLauncher in android?

A launcher for a previously-prepared call to start the process of executing an ActivityResultContract . Parameters. <I> type of the input required to launch.

How do I use registerForActivityResult?

registerForActivityResult() takes an ActivityResultContract and an ActivityResultCallback and returns an ActivityResultLauncher which you'll use to launch the other activity. An ActivityResultContract defines the input type needed to produce a result along with the output type of the result.


2 Answers

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)

like image 56
CommonsWare Avatar answered Sep 18 '22 00:09

CommonsWare


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'
    ...
}
like image 22
tronman Avatar answered Sep 20 '22 00:09

tronman