64

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?

Henry
  • 17,490
  • 7
  • 63
  • 98
CoDe
  • 11,056
  • 14
  • 90
  • 197

9 Answers9

140

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://stackoverflow.com/a/33331459/4747587

Community
  • 1
  • 1
Henry
  • 17,490
  • 7
  • 63
  • 98
89

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

implementation "androidx.fragment:fragment:1.3.4"
Saurabh Thorat
  • 18,131
  • 5
  • 53
  • 70
  • Thank you! You should really put this into a self-answered question so other people can find it easily. – LukeWaggoner Aug 07 '20 at 22:55
  • 3
    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' – Mina Samir Aug 13 '20 at 02:39
  • Why they told us how many dependencies required to have new features? Google docs sucks! Thanks by the way for helping. – Maulik Dodia Dec 11 '20 at 11:57
  • No idea why the error isn't more specific. At any rate, thank you. My hero <3 – Victor Ude Mar 26 '21 at 21:19
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.

vanomart
  • 1,739
  • 1
  • 18
  • 39
29

You need to pass a positive number to startActivityForResult.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Stephane Mathis
  • 6,542
  • 6
  • 43
  • 69
28

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'
Shaon
  • 2,496
  • 26
  • 27
13

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.

ONE
  • 567
  • 7
  • 15
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.

Tzegenos
  • 837
  • 12
  • 16
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.

1

Just add

for kotlin

implementation 'androidx.fragment:fragment-ktx:1.3.3'