How to avoid coupling of Activity and Fragment (MVVM/NavController)

68 Views Asked by At

So there is a case where I have a Single Activity Application with several fragments under it. Application uses:

  • MVVM architecture
  • Navigation Component for navigation within the app

I need to access NavController from fragment in order to do a navigation when a click is pressed. NavController is instatiated in Activity. I know by doing something like:

((NameOfTheActivity) getActivity()).myNavController

while myNavController is declared as public, i will get it into the fragment, but that is introducing coupling as far as I understand. Coupling is a bad thing when the whole concept of programming and MVVM is to modularize whenever possible to keep things clear and reusable.

Is there any better way of accessing that item from the activity? Or at least is there a better way of triggering the navigation?

0

There are 0 best solutions below