I recently learned how to make nested fragments in Android. I don't know how communication is supposed to happen, though.
From reading the fragment communication documentation I know that
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
This makes sense for sibling fragments within an activity, but it doesn't make as much sense for parent-child fragment communication. Do I need to go all the way up to the Activity just for the Child Fragment to talk to the Parent Fragment? If the answer is a simple "yes" then I can do that. If it is a "no", then what would the code design look like?
I see in the Nested Fragment documentation that one can use getParentFragment()
to get a reference to the parent fragment. So does that mean that the child should directly communicate with the parent? That seems opposite from what is encouraged with a normal fragment communicating with the parent activity.
Following Rahul Sharma's advice in the comments, I used interface callbacks to communicate up from the Child Fragment to the Parent Fragment and to the Activity. I also submitted this answer to Code Review. I am taking the non-answer there (at the time of this writing) to be a sign that there are no major problems with this design pattern. It seems to me to be consistent with the general guidance given in the official fragment communication docs.
Example project
The following example project expands the example given in the question. It has buttons that initiate upward communication from the fragments to the activity and from the Child Fragment to the Parent Fragment.
I set up the project layout like this:
Main Activity
The Activity implements the listeners from both fragments so that it can get messages from them.
Optional TODO: If the Activity wanted to initiate communication with the fragments, it could just get a direct reference to them and then call one of their public methods.
Parent Fragment
The Parent Fragment implements the listener from the Child Fragment so that it can receive messages from it.
Optional TODO: If the Parent Fragment wanted to initiate communication with the Child Fragment, it could just get a direct reference to it and then call one of its public methods.
Child Fragment
The Child Fragment defines listener interfaces for both the Activity and for the Parent Fragment. If the Child Fragment only needed to communicate with one of them, then the other interface could be removed.
Although @Suragch's answer is correct, But I want to add another way to pass data between
Fragments
orActivity
. No matter it's anActivity
orFragment
you can pass data with event bus in 3 steps:1- Define an event(message):
2- Register and Unregister for Events: To be able to receive events, the class must register/unregister for event bus. The best place for
Activities
andFragments
isonStart()
andonStop()
To be able to receive an event you have to subscribe to that event. To do that, add
@Subscribe
annotation to one of your methods in your class.3- Post an event
More documentation and examples could be found Here. There are also other libraries like : Otto