ActivityResultLauncher Crash — Can only use lower 16 bits for requestCode

Anum Amin
1 min readSep 15, 2020

--

I was trying a new way of writing runtime permissions on Android. It amazed me how precise the code has become, and you do not need to write hundreds of lines of boilerplate code.

In case you are trying to acquire permission from the user using new ActivityResultLauncher and ActivityResultContracts, you may get the crash mentioned in the title. As a solution, it is necessary to include following dependencies together even if they do not look relevant to each other, yet fixes the crash.

implementation 'androidx.activity:activity-ktx:1.2.0-alpha08'
implementation 'androidx.fragment:fragment:1.3.0-alpha08'

Thanks to Saurabh for pointing out. In case you would like to acquire multiple permissions at a time from the user, you can use the following code:

val multiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { map ->//Callback received what we used to get in onRequestPermissionsResult}multiplePermissions.launch(arrayOf(
Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE))

Looks cool. Right? Happy Coding!

--

--