0

There are lot of threads about how to make two Fragments communicate each other using an interface and event call back methods through Activity. Is there any specific reason to do that way? or is there any downside doing this way-directly calling method of fragment-2 from fragment-1

 ((Fragment2) (getActivity().getFragmentManager().findFragmentById(R.id.fragment2))).methodOfFragment2();
Praveena
  • 6,340
  • 2
  • 40
  • 53
  • Given some experience in android, I feel like the chance of some kind of failure in that line of code is pretty high. It also tightly couples 2 fragments to eachother (if one is present the other must also always be present) – Kevin DiTraglia Jun 25 '15 at 19:24
  • You are assuming that `fragment2` exists and is in the same activity as your first fragment. In many cases, it may not, because it has not been created yet, or it used to exist but was removed via a `FragmentTransaction`, or there is not enough screen space so that fragment will be shown by another activity, etc. – CommonsWare Jun 25 '15 at 19:28

2 Answers2

4

Unfortunately the activity-interface in many tutorials doesn't give people a good model for actually making their fragments modular.

What most people end up doing is making their activity-interface some central repository for hardcoding relationships between fragements. Thus, when I first started usnig fragments and read about this "model" I too wondered at its value, since I was just introducing a layer of indirection over my fragment relations.

overtime I learned or adapted patterns, like the Broadcast/Receiver model to my fragments. Now my fragments don't communicate through the activity-inteface, but instead broadcast events on an event-frequency with payloads through a local broadcast object, and other objects interested in those events can register to receive those events and data.

In other words, my fragments have events, and they publish those events, often with payloads, and other objects can register to receive notification and data from those events. If no one's listening, nothing happens. If I write some new object that want's to receive data from the event, my other fragments and even my broadcaster don't need to be aware of any specifics of how my new object was implemented.

If you want to learn more about the broadcast Receiver model Android has its own LocalBroadcastManger, or you can write your own lightweight one, and you can find some tutorials on how to use it here:

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

how to use LocalBroadcastManager?

Community
  • 1
  • 1
NameSpace
  • 10,009
  • 3
  • 39
  • 40
3

Because a fragment is suppose to be able to be reused. If you are calling a fragment that exists in one instance of your app and you reuse one of those fragments in another section of your app but not the other then guess what you just broke your app trying to call something that is not there.

Fragments should not know another fragment exists which is why all communication should go through an activity

tyczj
  • 71,600
  • 54
  • 194
  • 296