hasanghaforian
hasanghaforian

Reputation: 14042

Using "id" as requestCode for "startActivityForResult" from FragmentActivity

Android Docs say in about ID XML resource:

A unique resource ID defined in XML. [...] which you can use as [...] unique integer for use in your application code (for example, as an ID for a dialog or a result code).

I created request in ids.xml

<item name="request" type="id" />

Its value is -1000003. I want to use it as requestCode for startActivityForResult from FragmentActivity. But as it is described here, requestCode must be of 16 bits, meaning the range is from 0 to 65535. So the value of request is not valid (it is negative and also exceeds bounds range). and using it causes this error:

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

Although I can use arbitrary valid integer, but its uniqueness may be violated during time of application maintaining/developing. Is there a way to solve this problem?

Upvotes: 1

Views: 216

Answers (2)

G&#225;bor
G&#225;bor

Reputation: 10274

Use toShort() to convert it to a 16-bit value (or simply and with 0xFFFF). You can do it safely because the upper word only identifies the type of the ID, the lower word itself is the unique ID, anyway.

Upvotes: 0

Kartika Vij
Kartika Vij

Reputation: 211

Your value is a negative number but range is of positive numbers only. This error is might be because of this reason

Upvotes: 0

Related Questions