Android: what to choose for requestcode values?
Asked Answered
W

1

46

Methods like ActivityCompat.requestPermissions require that I pass them a requestcode that I can later test in a callback (in this case onRequestPermissionsResult). Is there some best practice sort of value I'm supposed to pass in the requestcode? I've noticed that if I just enter a random int I sometimes get an error like this:

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



10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: FATAL EXCEPTION: main
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: Process: my package, PID: 8315
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackage.myactivity}: java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:  Caused by: java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.validateRequestPermissionsRequestCode(FragmentActivity.java:799)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.support.v4.app.ActivityCompatApi23.requestPermissions(ActivityCompat23.java:29)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.support.v4.app.ActivityCompat.requestPermissions(ActivityCompat.java:316)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at mypackage.myactivity.checkReadPhoneState(PermissionsGatewayActivity.java:48)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at mypackage.myactivity.onCreate(PermissionsGatewayActivity.java:36)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
10-25 16:47:43.652 8315-8315/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Wandis answered 25/10, 2015 at 14:55 Comment(11)
Try request code between 1 to 255.Wergild
that's what it means? So theoretically you could never have more than 256 requests running in parallel? (ridiculous though that would be)Wandis
I think that number is sufficient in real world scenario.Wergild
There are less then 30 permission(Dangerous permissions) for that we require to call ActivityCompat.requestPermissions() method. So i think 256 is more than enough. isn't it?Wergild
I am just going through the android code for more info on this. Could you post your full stack trace ?Realize
yes, I know, but this limitation is for all methods that use requestcode in their callback, so not just permissions requests. Anyway - thank you very much for your helpWandis
@henry - many thanks, but no need as Dhaval already solved the issue for meWandis
Not for that, but just curious why it is 8 bits. I checked FragmentActivity and found 16 bits. So out of curiosity. Help me out. The complete stacktrace will help me locate where in source code this thing is put.Realize
@Realize - edited to include the stacktraceWandis
I think what pissed me off is that the request code parameter is an int , additionally, the documentation makes no damn mention of the 8 bit limit. FFSExpiation
Well a 8 bit request code works for me on the first start. but if i delte all app data in the app info I get the same error again when I launch the app from the phone, without changing the request code. Any Idea?Anticlerical
R
92

Documenting the findings for future reference:

The following are code from android.support.v4.app.FragmentActivity

 /**
 * Modifies the standard behavior to allow results to be delivered to fragments.
 * This imposes a restriction that requestCode be <= 0xffff.
 */
@Override
public void startActivityForResult(Intent intent, int requestCode) {
    if (requestCode != -1 && (requestCode&0xffff0000) != 0) {
        throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
    }
    super.startActivityForResult(intent, requestCode);
}


@Override
public final void validateRequestPermissionsRequestCode(int requestCode) {
    // We use 16 bits of the request code to encode the fragment id when
    // requesting permissions from a fragment. Hence, requestPermissions()
    // should validate the code against that but we cannot override it as
    // we can not then call super and also the ActivityCompat would call
    // back to this override. To handle this we use dependency inversion
    // where we are the validator of request codes when requesting
    // permissions in ActivityCompat.
    if (!mRequestedPermissionsFromFragment
            && requestCode != -1 && (requestCode & 0xffff0000) != 0) {
        throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
    }
}


RANGE
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.

Realize answered 25/10, 2015 at 15:31 Comment(3)
Range has changed. android.googlesource.com/platform/frameworks/support/+/…Bleacher
@Bleacher Thanks. Updated my answer.Realize
😢Only on Android. Maybe we can/should put a link to this answer in AOSP code comments, so that anyone who updates this code resolving logic in AOSP remembers to post an update here as well...Begot

© 2022 - 2024 — McMap. All rights reserved.