2

I'm trying to implement a multi-module app structure with navigation between modules. The challenge Im having is doing this with a structure that contains nested fragment containers.

Background: Module Structure The design uses a single activity.The main activity layout comprises of an actionbar and fragment container. This container is used to host the different 'main' module fragment.

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/frg_main_container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph_app" />

Each module has a single 'parent' like fragment with a fragment container and bottomNavbar. This fragment container is used to host the detail fragments of each module and is the home fragment in the modules nav graph.

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/frg_home_contacts_container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="false"
        app:navGraph="@navigation/nav_graph_contacts" />

Layout Example The app module contains a top level nav graph linking each modules parent fragment (these are not nested graphs or the modules nav graph, as the module's detail fragments are not used in the apps container view). Each module has a bottom level nav graph linking the detail fragments of that module. This is used in conjunction with the bottomNavbar to navigate between module fragments [works].

For example, the goal is to have a button, on the home screen of the home module, that when clicked moves the user to a screen within another module, such as the contacts list screen in the Contact module.

There are two actions:

  1. Navigate the Main container to the new module home fragment. (managed by the navContorller in the App)
  2. Navigate the module container to the new details fragment. (managed by the navController in the module)

Presently I'm working with interfaces and trying to inject (hilt) into the main activity to achieve no.1. However, I'm always running into issues that the home module cant see the apps nav_graph or there are missing dependencies (i.e. it wants the dependency to get the nav_graph). I believe a simlar process should work between fragments for no.2

Concept reference: https://itnext.io/multi-module-navigation-in-android-63cb9924ffbd

This following code is functional when R.id.nav_graph_home but for links outside the module i.e. R.id.nav_graph_contacts it isnt.

interface HomeModuleNavInterface {
    fun gotoContacts(navController: NavController)
}
class HomeModuleNavContract : HomeModuleNavInterface {
    override fun gotoContacts(navController: NavController) {
        navController.navigate(R.id.nav_graph_contacts)
    }
}
@Module
@InstallIn(SingletonComponent::class)
class HomeNavigator  {
   @Singleton
   @Provides
   fun providesHomeNavInterface(): HomeModuleNavContract = HomeModuleNavContract()
}
class FragmentHomeNavigation : Fragment(R.layout.fragment_home_navigation) {
...
   @Inject
   lateinit var homeModuleNavigation: HomeModuleNavContract
...
   btnContact.setOnCLickListener{
      homeModuleNavigation.gotoContacts(findNavController())
   }
}

Any suggestions or direction is appreciated. Thanks.

Cameron
  • 67
  • 5
  • Hi bro. How you've resolved for this ? I am facing the same question. – Thân Hoàng Dec 29 '21 at 07:18
  • Hi @ThânHoàng, unfortunatley no I didnt find a good solution. In the end I implementd a work around with seperate nav graphs and some basic top level navigation. I managed to be able to jump between modules (from a home screen) relatively easily but couldnt navigate to a specific screen within a module or pass data objects to that screen which I had hoped to be able to do. I still think this should be achievable but havent looked at it in a while now. – Cameron Jan 10 '22 at 05:35

0 Answers0