Wrong requestCode in onActivityResult
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When onActivityResult receives a different requestCode than the one you passed to startActivityForResult, the cause is almost always that you called startActivityForResult from a Fragment but the result was delivered to the parent Activity, or the requestCode was modified by the framework. In Fragments, the upper 16 bits of the requestCode are used internally to identify the fragment, so only the lower 16 bits (0-65535) are preserved. The modern fix is to use the Activity Result API (registerForActivityResult), which eliminates requestCode management entirely.
The Problem
Cause 1: Fragment requestCode Mangling
When a Fragment calls startActivityForResult, the hosting Activity adds the fragment's index to the upper 16 bits of the requestCode:
If onActivityResult is handled in the Activity instead of the Fragment, you see the modified code. If handled in the Fragment, the framework strips the bits and delivers the original code.
Fix: Let the Fragment Handle the Result
The critical line is super.onActivityResult() in the Activity. Without it, the Fragment's onActivityResult is never called.
Cause 2: Using requestCode > 65535
Only the lower 16 bits are available for your request code:
Using values above 65535 causes the upper bits to conflict with the fragment index bits.
Cause 3: Calling Activity's startActivityForResult from Fragment
When you call getActivity().startActivityForResult(), the Activity handles the result directly and the Fragment is bypassed.
Cause 4: Nested Fragments
Nested fragments (fragments inside fragments) have additional complications:
Fix for Nested Fragments
Modern Fix: Activity Result API (Recommended)
The Activity Result API eliminates requestCode entirely:
Common Activity Result Contracts
Kotlin Example
Debugging requestCode Issues
Common Pitfalls
- Not calling
super.onActivityResult()in the Activity: If the Activity overridesonActivityResultwithout callingsuper, the result is never forwarded to the Fragment. Always callsuper.onActivityResult(requestCode, resultCode, data)first. - Calling
getActivity().startActivityForResult()from a Fragment: This bypasses the Fragment's result dispatch. The result goes to the Activity'sonActivityResult, not the Fragment's. Always callstartActivityForResult()directly from the Fragment. - Using
requestCodevalues greater than 65535: The upper 16 bits are reserved for the fragment index. Using large request codes causes bit collisions and delivers wrong codes. Keep values between 0 and 65535. - Comparing the mangled
requestCodein the Activity: If you handle the result in the Activity instead of the Fragment, therequestCodeincludes fragment index bits. Either handle the result in the Fragment or mask withrequestCode & 0xFFFF(not recommended — use the Activity Result API instead). - Not migrating to the Activity Result API:
startActivityForResultandonActivityResultare deprecated since AndroidX Activity 1.2.0. The Activity Result API is type-safe, eliminatesrequestCodemanagement, and works correctly with Fragments and nested Fragments.
Summary
- The wrong
requestCodeinonActivityResultis usually caused by Fragment requestCode bit mangling (upper 16 bits store the fragment index) - Always call
super.onActivityResult()in the Activity to forward results to Fragments - Use
startActivityForResult()from the Fragment, notgetActivity().startActivityForResult() - Keep
requestCodevalues below 65536 (16 bits) - Migrate to
registerForActivityResult()(Activity Result API) to avoidrequestCodeissues entirely
Related reading
- Xamarin Gcm Network Manager await httpclient
- Xamarin.Android no stack trace in async method
- Xamarin.Forms ListView Set the highlight color of a tapped item
- xcode-select active developer directory error
- x-b3-sampled header is always set to 0 when accessing service through ingress controller
- X509Certificate Constructor Exception
- Xcode6 Run two instances of the simulator
- Xcode - Could not find test host
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.