I’d like my activity to send the same event to multiple fragment. Instead of making my activity calling each individual fragment method : FragmentA.DoTask(), FragmentB.DoTask(), FragmentC.DoTask(), etc… I’d like rather make my activity send only one event and then make the fragment listening to this event.
On the developpers docs they make the activity “listen” to the fragment but then the activity calls the fragments’ methods. Is it possible the other way around : to make the fragments “listen” to the activity.
Thanks
I don’t think there is a way around it. The word “listen” in the docs is more metaphorical. You have to call all fragments explicitly.
The closet thing you can get, is to have a list of Fragment maintained in your Activity class. e.g.: create a customized Fragment class:
Have all your Fragments inherited from this class.
In your Activity class, each time you create a Fragment, add it to a List too. When the event occurs, call all Fragments.
Note: If you don’t have that many Fragments, this solution can be a overkill.