OnActivityResult method is deprecated, what is the alternative?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Android development, handling the results of activities has traditionally been done using the onActivityResult method. However, this method is now deprecated, and developers are encouraged to use the new Activity Result APIs. This article will provide an in-depth look at why onActivityResult has been deprecated, and how to implement the new Activity Result APIs effectively.
The Deprecation of onActivityResult
Background
The onActivityResult method has been a staple in Android development for communicating between activities and fragments. It allows a source activity to start a target activity and receive a result back from it. However, this approach has several drawbacks:
- Tight Coupling: The method necessitates callback handling within the
ActivityorFragment, which can lead to tight coupling. - Complexity: Managing request and result codes becomes cumbersome as the app scales, making the codebase harder to maintain.
- Lifecycle Sensitivity: The method is sensitive to lifecycle changes—developers often have to manage complex state-saving logic to handle configuration changes or process death.
Due to these limitations, the Activity Result APIs were introduced to offer a more robust and modular solution.
Introducing: Activity Result APIs
The new Activity Result APIs provide a type-safe and lifecycle-aware approach to handle activity results. Here’s how the new API improves upon the deprecated method:
- Decoupling: Activities and Fragments handle results in a decoupled manner, avoiding bloated callback methods.
- Lifecycle Awareness: Results are delivered only when lifecycle conditions are safe, leveraging Jetpack’s lifecycle components.
- Simplified Code: It eliminates the need for custom request/result codes and reduces boilerplate code.
Implementation
1. Setting Up Dependencies
Ensure you have the necessary dependencies in your build.gradle file:
2. Registering for Activity Result
The new API uses ActivityResultLauncher to handle the results. You register this in the component (Activity or Fragment) using a registerForActivityResult call.
Example in a MainActivity:
3. Launching the Activity
Using this method, launching the activity is more explicit, focusing on the action (such as fetching content) rather than managing codes.
Key Benefits and Comparison
Below is a table summarizing the key differences between onActivityResult and the new Activity Result APIs:
| Feature | onActivityResult | Activity Result APIs |
| Coupling | Tight coupling through callbacks | Decoupled via ActivityResultLauncher |
| Code Complexity | High due to request code management | Low; uses type-safe contracts |
| Lifecycle Management | Developer-managed | Built-in lifecycle awareness |
| Error Handling | Manual error checking | Simplified, with reduced boilerplate |
| Reusability | Limited | Highly reusable and modular |
Additional Details
Handling Permissions
The new Activity Result APIs also extend to handling permissions more gracefully. For instance, instead of using the old requestPermissions method, you now use ActivityResultContracts.RequestPermission:
Custom Activity Result Contracts
The ActivityResultContracts class provides a set of predefined contracts, but you can also define custom contracts for complex operations. Here's a simple example:
Conclusion
The deprecation of onActivityResult is part of Android’s effort to encourage cleaner, more maintainable code through modern APIs. The new Activity Result APIs offer significant improvements in terms of decoupling, lifecycle awareness, and code simplicity. By leveraging these APIs, developers can ensure their applications are robust, modular, and aligned with the latest best practices in Android development.

