Android
Fragment
onActivityResult
Mobile Development
Troubleshooting

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:

java
1import android.app.Activity;
2import android.content.Intent;
3import androidx.activity.result.ActivityResultLauncher;
4import androidx.activity.result.contract.ActivityResultContracts;
5import androidx.fragment.app.Fragment;
6
7public class ProfileFragment extends Fragment {
8
9    private final ActivityResultLauncher<Intent> launcher =
10        registerForActivityResult(
11            new ActivityResultContracts.StartActivityForResult(),
12            result -> {
13                if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
14                    Intent data = result.getData();
15                    // Handle result here
16                }
17            }
18        );
19
20    private void launchEditor() {
21        Intent intent = new Intent(requireContext(), EditProfileActivity.class);
22        launcher.launch(intent);
23    }
24}

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:

java
Intent intent = new Intent(requireContext(), EditProfileActivity.class);
startActivityForResult(intent, 1001);

Do not do this if you expect the fragment callback:

java
// Avoid this for fragment result handling
getActivity().startActivityForResult(intent, 1001);

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:

  1. which component launched the activity
  2. whether the fragment is still attached when the result returns
  3. whether the target activity actually calls setResult(...)
  4. whether the result code is RESULT_OK
  5. 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 an ActivityResultLauncher.
  • 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.

Course illustration
Course illustration

All Rights Reserved.