https://kotlinlang.org logo
#compose
Title
# compose
j

Jasmin Fajkic

08/24/2022, 7:04 PM
me once again with navigation 😄 😅. Is it better in navhost to create one function navigate and just where we need pass string as route where to go (check thread):
Copy code
@Composable
fun NavigationHost(navController: NavHostController) {
    NavHost(
        navController = navController,
        startDestination = NavRoutes.Feed.route,
    ) {
        fun navigate(route: String) {
            navController.navigate(route)
        }

        composable(NavRoutes.Feed.route) {
            CommunityFeed(
                navigate = { route: String -> navigate(route) },
            )
        }

        composable(NavRoutes.Discover.route) {
            Discover(
                navigate = { route: String -> navigate(route) },
            )
        }

        composable(NavRoutes.Inbox.route) {
            Inbox(
                navigate = { route: String -> navigate(route) },
            )
        }

        composable(NavRoutes.TimeLine.route) {
            Timeline(
                navigate = { route: String -> navigate(route) },
            )
        }
        composable(NavRoutes.CreatePost.route) {
            CreatePost(
                navigate = { route: String -> navigate(route) },
            )
        }
    }
}
or in navhost create as many as needed functions for each composable and define how they behave. Second solution is possible a bit strange as if my screen is very complex I could easily have in navhost 10 functions that I would pass to my composable.
i

Ian Lake

08/24/2022, 7:25 PM
Your screens shouldn't know about Navigation or routes at all - they should be entirely type safe. Here's one of the previous discussions where we talked about structure: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1645595702068129?thread_ts=1645552485.247699&cid=CJLTWPH7S
6 Views