Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActivityResultLauncher successfully launch the ActivityResultContracts the first time on AppCompatActivity , but crashes at the second time [duplicate]

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?

like image 911
CoDe Avatar asked Nov 17 '25 18:11

CoDe


2 Answers

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

like image 58
Henry Avatar answered Nov 19 '25 09:11

Henry


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

implementation "androidx.fragment:fragment:1.3.4"
like image 37
Saurabh Thorat Avatar answered Nov 19 '25 10:11

Saurabh Thorat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!