Communicate_between_two_fragments_via_interface.tx...
# getting-started
i
Communicate_between_two_fragments_via_interface.txt
m
why just use 2 regular activities instead?
i
no they are in two different activities
I need to call the "MainFragment" to refresh the view
and so i need a callback in the Second Fragment to say, hey close this fragment and
activity.supportFragmentManager.popBackStack()
and refresh the widget in the MainFragment
m
you can get access to specific fragment from activity like
Copy code
val fragments = activity.supportFragmentManager.fragments
for f in fragments{
if f is InterfaceSomething{
f.doThatThing()
}
}
i
I cannot use the activity, in my use case
communication have to be between fragments because of the architecture choice
m
are you also using some sort of event bus or rxjava?
i
rxjava2
m
ok, then you can easily introduce Rx based publish-subscribe pattern https://blog.mindorks.com/implementing-eventbus-with-rxjava-rxbus-e6c940a94bd8
i
thank you
m
one fragment can send events, and other one listens
i
looks good thanks
m
btw, what architecture do you use? I wonder there's cleaner approach ...
i
MVVM
I have an overview that communicates with 3 other fragments
m
ok, I see. so you can create singleton dependency with a RxBus structure and inject for each viewmodels
i
do you have a link?