New result API error : Can only use lower 16 bits for requestCode New result API error : Can only use lower 16 bits for requestCode android android

New result API error : Can only use lower 16 bits for requestCode


Add or update to this dependency:

implementation 'androidx.fragment:fragment:1.3.0'

When using ActivityResult APIs, use this fragment dependency to ensure that FragmentActivity is compatible.


I think something is wrong with the new API. In ActivityResultRegistry you can see :

 /** * Generate a random number between the initial value (00010000) inclusive, and the max * integer value. If that number is already an existing request code, generate another until * we find one that is new. * * @return the number */private int generateRandomNumber() {    int number = mRandom.nextInt((Integer.MAX_VALUE - INITIAL_REQUEST_CODE_VALUE) + 1)            + INITIAL_REQUEST_CODE_VALUE;    while (mRcToKey.containsKey(number)) {        number = mRandom.nextInt((Integer.MAX_VALUE - INITIAL_REQUEST_CODE_VALUE) + 1)                + INITIAL_REQUEST_CODE_VALUE;    }    return number;}

In debugged mode i saw "1388473134" sent for request code. Now lets see what is happening. In this case we are using ActivityResultRegistry providedby the framework. This generate a request code hold by the framework and generated by generateRandomNumber from ActivityResultRegistry.

See in ComponentActivity class of google framework androidx.activity.

private ActivityResultRegistry mActivityResultRegistry = new ActivityResultRegistry() {    @Override    public <I, O> void onLaunch(            final int requestCode,            @NonNull ActivityResultContract<I, O> contract,            I input,            @Nullable ActivityOptionsCompat options) {            ...                    } else {            // startActivityForResult path            ActivityCompat.startActivityForResult(activity, intent, requestCode, optionsBundle);        }}        

This will call checkForValidRequestCode from FragmentActivity and throw an exception (because the request code generate is too "hight").

@Override    public void startActivityForResult(@SuppressLint("UnknownNullness") Intent intent,            int requestCode, @Nullable Bundle options) {        // If this was started from a Fragment we've already checked the upper 16 bits were not in        // use, and then repurposed them for the Fragment's index.        if (!mStartedActivityFromFragment) {            if (requestCode != -1) {                checkForValidRequestCode(requestCode);            }        }        super.startActivityForResult(intent, requestCode, options);    }

To remove the issue you have to update the source of checkForValidRequestCode provided in androidx.fragment.app in your build gradele with a fresh version of this method.

In your gradle file

dependencies {    def fragment_version = "1.3.0-beta02"    // Java language implementation    implementation "androidx.fragment:fragment:$fragment_version"    // Kotlin    implementation "androidx.fragment:fragment-ktx:$fragment_version"    // Testing Fragments in Isolation    debugImplementation "androidx.fragment:fragment-testing:$fragment_version"}