Sometimes it can be challenging to send information to a specific fragment without creating a tight coupling.
This can be done with an EventBus, but architecture wise this will create its own problems (a high danger of deeply spaghettified code which is hard to reason about is just one of them).
A better way would be to add something like this to your message dispatching Activity:
data class Message(val someData: String)
interface MessageReceiver {
fun onReceiveMessage(message: Message)
}
private fun nofityMessageReceivers(message: Message) {
supportFragmentManager.fragments
.filterIsInstance(MessageReceiver::class.java)
.forEach { receiver ->
receiver.onReceiveMessage(message)
}
}
Rename the MessageReceiver into something more descriptive and make sure your receiving Fragments implement the interface and you are good to go!