Member-only story
Android: Crash on Android — Can only use lower 16 bits for requestCode
Overview
The error message “Can only use lower 16 bits for requestCode” in Android occurs when you’re trying to use a request code that exceeds the limit of 16 bits (which means it should be less than 65536). This is common in intents, especially when starting activities for results or using startActivityForResult()
or ActivityResultLauncher
.
Root cause
Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
Documenting the findings for future reference:
The following are code from android.support.v4.app.FragmentActivity
RANGEstartActivityForResult()
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.
Understanding the Request Code
In Android, the request code is an integer you provide to uniquely identify the request when you start an activity for a result. It should be a value between 0…