
The modern way to pass data between fragments
Pass data through Fragment Manager
With Android Jetpack components a lot of things are happening here and there. Nowadays most of the developers tend to use Single Activity architecture, with that communicating between fragments become more crucial.
There are multiple ways to communicate between fragments. For example, using Interfaces or a shared ViewModel. But, this is sometimes additional overhead. As an alternative setTargetFragment
could be used but, this is also now deprecated.
So, let's look at the modern way.
The modern way
Beginning from Fragment release 1.3.0-alpha04
and onwards FragmentManager
can be used to communicate between fragments. Now FragmentManager
by default implements from FragmentResultOwner
and override the methods to set and clear fragment results and fragment result listeners.
How it works
The FragmentManager
holds fragment results and fragment result listeners in separate Maps
with requestKey
as a unique identifier. The fragment result listener which registered to the relevant requestKey
get notified when the result is set or updated.
This way it helps to communicate between fragments in the same FragmentManager
and also child to parent fragment or vice versa.
To pass data between fragments in the same fragment manager, the listener should be added to the destination fragment with requestKey
in order to receive the result produces from another fragment with the same key.
Now, the source fragment should produce the result on the same FragmentManager
with the same requestKey
as follows:
That's it, the destination fragment will receive the result and execute listener callback once it's STARTED.
To pass data from child to parent fragment, should follow the same way by using childFragmentManager
instead parentFragmentManager
when set result listener to the parent fragment.
Now, same as the previous example, child fragment should produce the result with the same requestKey.
Yes, all you need. The parent fragment will receive the result immediately. If communication requires to be done other way around (pass data from parent to child fragment), the result listener should be set to child fragment and result should be produced from parent fragment. Then don't forget to use childFragmentManager
instead parentFragmentManager
when producing the result from the parent fragment.
Points to keep in mind
- Only a single listener and result can be registered to the given key. If more than one listener is registered for the same key, it will be replaced for the latest one.
- If destination fragment in back stack, and calls
setFragmentResult()
from source fragment more than once for the same key, the system sends the most recent result to the destination fragment before the source fragment is popped off the back stack. - If the result is set without having listener to receive it, the result is stored in the
FragmentManager
until the listener is set with the same key. When the listener is set and the fragment isSTARTED,
it will receive the result immediately. - The result will be cleared from
FragmentManager
once the destination fragment receives it.
The example project can be seen here and more info here.
Hope you learn something useful. See you in the next one ✌️