onActivityResult is not being called in Fragment
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If onActivityResult() is not being called in a Fragment, the usual cause is that the result is being launched or routed through the wrong component. On modern Android, the deeper truth is that onActivityResult() is legacy API and the recommended solution is to use the Activity Result APIs with registerForActivityResult().
Why the Old Pattern Fails
Historically, fragments could call startActivityForResult() and receive a callback in onActivityResult(), but this approach was fragile. Common failure modes included:
- starting the activity from
getActivity()instead of the fragment - nested fragment routing confusion
- fragment replacement or detachment before the result returns
- lifecycle timing issues after configuration changes
That is why the modern recommendation is to stop depending on onActivityResult() for new code.
The Modern Solution: registerForActivityResult()
The Activity Result APIs are lifecycle-aware and work much better with fragments:
This is the default answer for current fragment-based Android code.
If You Are Stuck on the Legacy API
If you are maintaining older code and still using onActivityResult(), make sure the fragment starts the activity itself:
Do not do this if you expect the fragment callback:
That sends the result flow through the activity path rather than the fragment path you probably intended.
Nested Fragments Make Legacy Routing Worse
Nested fragments are one of the reasons the old API became painful. If a child fragment launched the activity but the host activity or parent fragment handled the callback differently, the result could appear to vanish.
The Activity Result APIs reduce that ambiguity because the launcher is registered directly with the lifecycle owner that needs the result.
Lifecycle Timing Matters
Another subtle issue is registration timing. With registerForActivityResult(), register once as part of fragment initialization, not dynamically at arbitrary moments after the lifecycle has advanced unpredictably.
That gives the framework a stable callback target across recreation events such as rotation.
A Better Mental Model
The old model was "launch and later receive an integer request code callback." The modern model is "register a result handler tied to this lifecycle owner, then launch through that handler."
That change is important because fragments are lifecycle-driven components, and the new API matches that reality better.
Troubleshooting Checklist
If the result still does not arrive, check:
- which component launched the activity
- whether the fragment is still attached when the result returns
- whether the target activity actually calls
setResult(...) - whether the result code is
RESULT_OK - whether you are using the old API unnecessarily
That checklist catches most real failures quickly.
Common Pitfalls
- Launching the result flow from the activity when the fragment expects to receive it.
- Continuing to build new code around deprecated
onActivityResult()patterns. - Replacing or detaching the fragment before the result can be delivered.
- Forgetting that the target activity must call
setResult(...)before finishing. - Trying to reason about nested-fragment result routing with the old API instead of using Activity Result APIs.
Summary
- '
onActivityResult()in fragments is legacy and often fragile.' - The recommended approach is
registerForActivityResult()with anActivityResultLauncher. - If you still use the old API, launch from the fragment itself, not through the activity.
- Lifecycle and fragment attachment state both affect result delivery.
- For modern Android fragment code, Activity Result APIs are the right default.

