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

AndroidStart Activity

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

implementation "androidx.fragment:fragment:1.3.4"

Solution 3 - Android

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.

Solution 4 - Android

You need to pass a positive number to startActivityForResult.

Solution 5 - Android

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'

Solution 6 - Android

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.

Solution 7 - Android

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.

Solution 8 - Android

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.

Solution 9 - Android

Just add

for kotlin

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCoDeView Question on Stackoverflow
Solution 1 - AndroidHenryView Answer on Stackoverflow
Solution 2 - AndroidSaurabh ThoratView Answer on Stackoverflow
Solution 3 - AndroidvanomartView Answer on Stackoverflow
Solution 4 - AndroidStephane MathisView Answer on Stackoverflow
Solution 5 - AndroidShaonView Answer on Stackoverflow
Solution 6 - AndroidONEView Answer on Stackoverflow
Solution 7 - AndroidTzegenosView Answer on Stackoverflow
Solution 8 - AndroidGbindinazeezView Answer on Stackoverflow
Solution 9 - AndroidIvantsov NickView Answer on Stackoverflow