androidandroidxandroid-architecture-componentsnavigation-architecture

Communicating between Activities and Fragments when using Navigation Architecture Component


I want to let the host Activity know when something happens in a Fragment. Traditionally, I would have an interface with a callback that the Fragment can call, but now we are ofc using the navigation architecture component.

Is there a way to pass a reference to the host activity down to the fragments or how would I otherwise solve the "Communication between activities and fragments" situation?

Thanks!


Solution

  • You can use the LiveData data holder class for such purposes.

    Here is an article explaining both Fragment <--> Fragment communication and Activity <--> Fragment communication.