java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
Asked Answered
A

9

65

I am writing an application where Activity A launches Activity B using

startActivityForResult(intent, -101);

but when called, it responded back with following error log:

E/AndroidRuntime( 1708): java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
E/AndroidRuntime( 1708):    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:837)

Probably it could be -101 but I am not sure. Does any one have any idea on this?

Alkahest answered 27/8, 2014 at 14:27 Comment(3)
Change -101 to a positive number?Weeping
Can only use lower 16 for requestCode : means request code should be >0 try startActivityForResult(intent, 101);Octodecimo
Try this solution :https://mcmap.net/q/297753/-generate-16-bit-unique-ids-in-android-for-startactivityforresultHeft
A
29

You need to pass a positive number to startActivityForResult.

Amylose answered 27/8, 2014 at 14:32 Comment(1)
Although this is true, answers below are more specific. +1Wretch
F
141

You get this exception only in android.support.v4.app.FragmentActivity and not when you use android.app.Activity.

startActivityForResult() in FragmentActivity requires the requestCode to be of 16 bits, meaning the range is from 0 to 65535.

Also, validateRequestPermissionsRequestCode in FragmentActivity requires requestCode to be of 16 bits, meaning the range is from 0 to 65535.

For more info(if you want to see the source code) : https://mcmap.net/q/297754/-android-what-to-choose-for-requestcode-values

Falter answered 25/10, 2015 at 17:6 Comment(0)
B
91

If you're using ActivityResult APIs, add this dependency to fix this issue:

implementation "androidx.fragment:fragment:1.3.4"
Bolo answered 7/7, 2020 at 12:35 Comment(4)
Thank you! You should really put this into a self-answered question so other people can find it easily.Phallicism
somehow and I really do not know why. but, You should add these two dependencies together. implementation 'androidx.activity:activity-ktx:x.x.x' implementation 'androidx.fragment:fragment-ktx:x.x.x'Mazda
Why they told us how many dependencies required to have new features? Google docs sucks! Thanks by the way for helping.Chiba
No idea why the error isn't more specific. At any rate, thank you. My hero <3Mg
H
35

It is also good to mention that this may happen if you use a number greater than 2^16 / 2 (which is 32768), so there's basically 2^15 options to not to screw this up.

Explanation: 16 bits can represent one of 65536 numbers, however, half of them are negative.

Henrietta answered 28/2, 2015 at 15:50 Comment(2)
The limit is 65535 and not 32768.Falter
@Falter Yes, negatif numbers are unusedPellicle
A
29

You need to pass a positive number to startActivityForResult.

Amylose answered 27/8, 2014 at 14:32 Comment(1)
Although this is true, answers below are more specific. +1Wretch
R
29

For those who are using the new ActivityResult API,

If you are using the new way (ActivityResult) to open new Activity.

registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { 
    result ->
}

you have to use both of the below dependency

implementation 'androidx.activity:activity-ktx:1.2.0-rc01'
implementation 'androidx.fragment:fragment-ktx:1.3.0-rc02'
Rembert answered 28/10, 2020 at 17:49 Comment(4)
It's kind of odd to use fragment implementation when not using it. But it works with itCrisscross
I think stable version will solve the problemRembert
@Rembert i have to disappoint you.. they dontLaoighis
This whole changes just messing things more and more.Verner
V
14

You can only use lower 16 bits for requestCode means -- in binary terms -- you can use

0000000000000000 (16 bits) to 1111111111111111 (16 bits) [ binary ].

Or, equivalently

0 to 65535 [ base 10 ].

In decimal ("number") terms, this allows for 2^16 = 65536 combinations. Therefore, you can only use the numbers 0 all the way through 65535.

You also cannot use negative numbers.

Viquelia answered 30/9, 2016 at 10:52 Comment(0)
A
4

The right answer is that you should use a 16 bits number for this purpose. The most safe solution for that is to always set your request code as short. If programmer attempts to write number more than 16 bits then the IDE won't let you to proceed because there will be an error.

Assume answered 27/9, 2018 at 8:30 Comment(0)
D
2

Just add the two main dependencies for activityforresult API

for kotlin

implementation 'androidx.activity:activity-ktx:1.3.0-alpha03'
implementation 'androidx.fragment:fragment-ktx:1.3.0'

for java

implementation 'androidx.activity:activity:1.3.0-alpha03'
implementation 'androidx.fragment:fragment:1.3.0'

check here for the latest version.

Distant answered 28/2, 2021 at 15:30 Comment(0)
G
1

Just add

for kotlin

implementation 'androidx.fragment:fragment-ktx:1.3.3'
Gnome answered 23/4, 2021 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.