Hi folks. Can anyone offer advice on best practice...
# compose
c
Hi folks. Can anyone offer advice on best practice for constructing nested navigation in a use case like this? I have an activity which provides behaviour based on some user-selected plugins. So the user can navigate to a “plugin screen”, at which point the UI content is provided by a plugin. I would like to define some nested navigation within the plugin, and I’m trying to figure out the best way to do this. My idea so far is to do something roughly like this:
Copy code
@Composable
fun MainNavHost(
  navController: NavHostController,
  plugin: Plugin,
) {
  NavHost(...) {
    // Define regular navigation within the activity
    composable(...) {
      ...
    }
    // Call this here to define nested navigation for plugin(s)
    plugin.navGraph(navController)
  }
}

interface Plugin {
  fun NavGraphBuilder.navGraph(navController: NavHostController)
  ...
}
This seems fine to me, though I don’t like passing
navController
down to the plugin. But it isn’t obvious to me how to avoid this, since there will be navigation to do within the plugin. But… does this seem sensible? Any risks stand out to you that I may not have considered? Any feedback is appreciated, thanks!
p
how I like to achieve this is my application listening events from this plugged UI, and handle the navigation in a centralized way from the app
for instance you can pass a lambda to the plugin like
navigationEvent: (String) -> Unit
and listen for these events in your app and decide what to do
c
ok so it could be like (updated from the code above):
Copy code
@Composable
fun MainNavHost(
  navController: NavHostController,
  plugin: Plugin,
) {
  NavHost(...) {
    ...
    // Call this here to define nested navigation for plugin(s)
    plugin.navGraph { route ->
      navController.navigate(route)
    }
  }
}
interface Plugin {
  fun NavGraphBuilder.navGraph(navigate: (String) -> Unit)
  ...
}
p
yes
c
ok yes I like this idea more! Thanks for responding
👍 1
Simillary question in discussion