Hello guys, There should be multiple screens in on...
# compose
t
Hello guys, There should be multiple screens in one screen. For example: in screen X, there will be screens A, B, C, D. But they all belong to X. Do I need to keep these screens as content or should I add them as navigation?
z
You mean like in a ViewPager, or like in tabs?
t
Actually, it’s not exactly like that. For example, I have a New Post screen. On this screen, you first set the location you are currently in. Then you press the done button and move to the second screen. On this screen, you set the location you are going to. Then, again, you press the done button and set how you are going to get there. Then, again, you press the done button and another screen comes up where you select other features and press the done button again and another screen. I need to navigate between these screens or is there a better way? All of these screens are part of the New Post and all use the New Post View Model.
d
Perhaps you want to check subgraphs for Navigation component. Sharing a VM or not is fully up to you, Compose will let you do both. Separate "screens" will be cleaner, from there you decide if you want to use nav args, share a VM or use a Repository (for example) to keep the shared data
t
Copy code
@OptIn(ExperimentalAnimationApi::class)
fun NavGraphBuilder.authNavigation(
    modifier: Modifier = Modifier,
    navController: NavController,
) {
    navigation(
        route = Graph.NEW_POST,
        startDestination = NewPostRoutes.From.route,
    ) {

        composable(route = NewPostRoutes.From.route) {
            FromRoute(
                modifier = modifier,
                navController = navController
            )
        }

        composable(route = NewPostRoutes.To.route) {
            ToRoute(
                modifier = modifier,
                navController = navController,
            )
        }

        composable(route = NewPostRoutes.Direction.route) {
            DirectionRoute(
                modifier = modifier,
                navController = navController,
            )
        }

        composable(route = NewPostRoutes.NewLocationRoute.route) {
            NewLocationRoute(
                modifier = modifier,
                navController = navController,
            )
        }

        composable(route = NewPostRoutes.Price.route) {
            PriceRoute(
                modifier = modifier,
                navController = navController,
            )
        }

    }
}

sealed class NewPostRoutes(val route: String) {
    @Immutable
    object From : NewPostRoutes("From")
    @Immutable
    object To : NewPostRoutes("To")
    @Immutable
    object Direction : NewPostRoutes("Direction")
    @Immutable
    object NewLocation : NewPostRoutes("NewLocation")
    @Immutable
    object Price : NewPostRoutes("Price")
}
How can I share the viewmodel in this structure? if there is a way to share the required viewmodel, I think doing it this way would create a much cleaner structure.
I am currently changing the contents within the scaffold in a single scene
i
viewModel
and
hiltViewModel
let you pass in what scope you want to use - if you want to use something scoped to the navigation graph, you'd pass it
navController.getBackStackEntry(Graph.NEW_POST)
as per the documentation: https://developer.android.com/jetpack/compose/libraries#hilt-navigation
t
It is exactly what i need. Thank you guys 🙏