Receive result from DialogFragment
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The modern Android answer for receiving a result from a DialogFragment is usually the Fragment Result API. It lets the dialog publish a one-time Bundle result through the FragmentManager, and the host fragment or activity listens for that result without needing a brittle direct reference or custom cast-heavy listener interface.
The Recommended Pattern: Fragment Result API
If the dialog is returning a small one-time result, set a listener in the receiving fragment before showing the dialog.
Receiver fragment in Kotlin:
The dialog sends the result:
This keeps the dialog and host loosely coupled and works well for one-shot responses.
Why This Is Better Than the Old Listener Pattern
Older Android code often used an interface like:
- dialog defines
OnResultListener - activity or fragment implements it
- dialog casts the host and calls back directly
That works, but it has weaknesses:
- more boilerplate
- runtime cast failures
- tighter coupling between dialog and host
- more awkward lifecycle behavior during recreation
The Fragment Result API removes most of that friction for ordinary dialog results.
Activity Hosts Can Use the Same Idea
If the dialog is hosted directly by an activity, register the listener on the activity’s supportFragmentManager instead of a fragment manager owned by another fragment.
The principle is the same:
- listener registered on the relevant
FragmentManager - dialog sets the result on that same manager
The key is that both sides use the same result key and the same manager scope.
Use a Shared ViewModel for Ongoing Shared State
If the dialog is part of a longer-lived interaction rather than a one-time response, a shared ViewModel may be a better fit. The Fragment Result API is ideal when the result is:
- small
- one-time
- naturally representable in a
Bundle
If you need stream-like updates or more complex shared state, a shared ViewModel is usually cleaner.
Be Careful About the Right Fragment Manager
One of the easiest mistakes is mixing up:
- '
parentFragmentManager' - '
childFragmentManager'
If the host listens on one manager and the dialog posts the result on another, nothing happens and the code looks mysteriously correct.
A useful rule is:
- show the dialog with a manager
- listen for the result on that same manager
That keeps the communication path consistent.
Java Example
The same pattern works in Java:
And from the dialog:
So the idea is not Kotlin-specific. It is a Fragment API pattern.
Common Pitfalls
- Using different result keys between the sender and receiver.
- Posting the result on a different
FragmentManagerthan the one the listener is watching. - Reaching for a custom listener interface when the result is just a simple one-time
Bundle. - Trying to pass large or complex objects that do not belong in a fragment result bundle.
- Forgetting to register the result listener before the dialog result is sent.
Summary
- For modern Android apps, the Fragment Result API is usually the best way to receive a result from a
DialogFragment. - Register a listener on the correct
FragmentManager, then have the dialog callsetFragmentResult. - Use the same result key and the same manager on both sides.
- Prefer this over older listener interfaces for simple one-time dialog responses.
- Use a shared
ViewModelinstead when the communication is ongoing or more stateful than a single result.
Related reading
- Recycler view showing single item
- RecyclerView - Get view at particular position
- RecyclerView and java.lang.IndexOutOfBoundsException Inconsistency detected. Invalid view holder adapter positionViewHolder in Samsung devices
- RecyclerView blinking after notifyDatasetChanged
- RecyclerView Inconsistency detected. Invalid item position
- RecyclerView inside ScrollView is not working
- recyclerview No adapter attached; skipping layout
- Recyclerview not call onCreateViewHolder
.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.