onActivityResult is not being called in Fragment
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working with Android Fragments and Activities, a common task is handling the results of an activity after it is finished. Usually, developers rely on the onActivityResult() method to handle these results. However, there are occasions when the onActivityResult() may not be called in a Fragment, which can lead to frustrating debugging sessions. Understanding why this happens and knowing how to resolve it is crucial for robust Android app development.
Understanding Why onActivityResult() May Not Be Called
The primary reasons for onActivityResult() not being invoked in a Fragment generally relate to how the Fragment and the starting Activity are managed by the Android system. Here’s a deeper technical explanation:
- Fragment not using
startActivityForResult()correctly: If a Fragment callsstartActivity()instead ofstartActivityForResult(), thenonActivityResult()will never be called, as the system does not expect any result back. - Hosting Activity consumes the result: If the parent Activity of the Fragment has overridden
onActivityResult()but does not properly pass the results back to the Fragment, then the Fragment'sonActivityResult()will not be executed. - Incorrect request or result codes: The
onActivityResult()method checks therequestCodeandresultCodeto determine how to handle the return. If these codes aren't matched in the child Fragment or Activity, then the method won’t trigger the expected behavior. - Changes in Fragment Lifecycle: If the Fragment is removed or replaced, and not properly managed (state not saved and restored), then it might not receive the result if the
onActivityResult()is called after the Fragment is no longer active or in a proper state to receive the result. - Changes with Android Updates: With newer versions of Android, especially after Android P, behavior changes around
startActivityForResult()andonActivityResult()may affect older implementations, requiring updates or migration to newer APIs likeActivityResultLauncher.
How to Ensure onActivityResult() Gets Called
Implementing the following strategies can prevent this issue:
- Directly start
startActivityForResult()from the Fragment: Always usegetActivity().startActivityForResult()from the Fragment directly if intending to capture the result back in the Fragment. Make sure the intent is correct and the right codes are being used. - Properly manage parent Activity and Fragment relationship: Make sure that the parent Activity funnels the result back to the Fragment. This means handling the result at the Activity level using
super.onActivityResult()to allow the framework to manage the forwarding of results to child Fragments. - Lifecycle management: Ensure that Fragments are stable and persist state during Activity results operations. Using methods such as
setRetainInstance(true)or appropriately handlingonSaveInstanceState()andonRestoreInstanceState()can be pivotal. - Adopt newer APIs: Consider using the new
ActivityResultContractsAPI introduced in Jetpack to handle the result, which provides a more robust way to manage activity results with less boilerplate and more control over lifecycle and state.
Transitioning to ActivityResultLauncher
Moving to ActivityResultLauncher as part of Android Jetpack's Activity component simplifies the start activity for result process. Here's a brief implementation showing how to use it within a Fragment:
- Declare the
ActivityResultLauncher:
- Initialize it within
onCreate()or similar:
- Launch an Activity:
Summary Table
| Issue | Cause | Solution |
onActivityResult() not called | Fragment uses startActivity() | Use startActivityForResult() |
| Result not received in Fragment | Activity intercepts result | Ensure Activity forwards result using super.onActivityResult() |
| Lifecycle issues | Fragment not active or state not handled | Manage state and ensure Fragment is active |
| Compatibility | Older API issues | Migrate to ActivityResultContracts and use ActivityResultLauncher |
By understanding these elements and implementing effective solutions, you can ensure that your Android Fragments correctly handle results from other Activities, leading to a smoother and bug-free user experience.
Related reading
- One Spring Boot project, deploy to both JAR or WAR
- OnItemCLickListener not working in listview
- Only using @JsonIgnore during serialization, but not deserialization
- OpenJDK availability for Windows OS
- onActivityResult is not being called in Fragment
- OnActivityResult method is deprecated, what is the alternative?
- Operator overloading in Java
- Optimal JVM settings for Cassandra

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the 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.