How to manage startActivityForResult on Android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Android development, the startActivityForResult() method has been a fundamental way of managing inter-activity communication. Specifically, it allows one activity to start another for some result, then receive a callback when the result is returned. However, with newer updates in Android development, including the introduction of Jetpack libraries and changes to the activity and fragment APIs, it's vital to understand both the legacy and modern approaches to handling activity results.
Understanding startActivityForResult()
Traditionally, the method startActivityForResult() is used when an activity wants to receive a result from another activity. For instance, you might want to pick a photo from a gallery app or get a user's choice from another activity.
An example usage is as follows:
Here, PICK_PHOTO_REQUEST is an integer constant that identifies this specific request and will be used later to handle the result.
Receiving the Result
To handle the result, you override onActivityResult() in your activity:
In this method, requestCode helps you identify from which request you're processing the result, resultCode tells you if the operation was successful (RESULT_OK), and the data Intent can carry results data.
Migrating to the New ActivityResult API
Given some inherent issues and modern app architecture changes with startActivityForResult(), Google introduced a more robust way to handle activity results with the ActivityResult APIs. The API shifts the focus from handling results in the prescribed onActivityResult callback to handling them as lambda expressions or callbacks at the point of launching the activity.
Basics of the ActivityResult API
Instead of the old approach, now you initiate an activity result with a more specific contract and handle the result with a callback:
- Register the Activity Result: This is typically done in your activity or fragment's initialization logic.
- Launch the Activity: Activating the registered launcher when needed.
Benefits of the ActivityResult API
The new approach leverages a more modular structure that eases the handling of permissions and improves the readability and maintainability of the code:
- Clarity and Safety: Reduces boilerplate and makes it clearer when reading through the code where the result is handled.
- Lifecycle Awareness: Handling results in a way that is lifecycle-aware ensures less chance of memory leaks.
Summary Table
| Feature | startActivityForResult() | ActivityResult API |
| Handling method | onActivityResult() | Callback within method invoker |
| Code structure | Monolithic with switch-case | Modular with specific callbacks |
| Lifecycle awareness | Low | High |
| Integration with fragments | Complicated | Simplified |
Conclusion
Although startActivityForResult() is widely used and familiar to most Android developers, the new ActivityResult APIs offer a more robust and streamlined approach to handling results from activities. It's advisable to migrate to this new paradigm to take advantage of its lifecycle awareness and modular callback structure, aligning with modern Android development practices.
Related reading
- How to manage startActivityForResult on Android
- How to manually include external aar package using Gradle for Android
- How to merge two sorted arrays in Swift?
- How to Navigate from one View Controller to another using Swift
- How to open a URL in Swift?
- How to open a URL in Swift?
- How to open mail app from Swift
- How to open mail app from Swift
.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.