Blog Infos
Author
Published
Topics
Published
@Composable
fun MyAppNavHost(
    navController: NavHostController,
    modifier: Modifier,
) {
    NavHost(
        navController = navController,
        startDestination = Destinations.NewsListScreen.route,
        modifier = modifier,
    ) {
        // bottom navigation screens & nested graphs
        newsListGraph(navController)
        favoriteNewsGraph(navController)
        profileGraph(navController)

        // common screens in entire app
        newsDetailGraph()

        ...
    }
}

For prevent to create a huge main navGraph, we split it to separate screens and nested graphs. In fact, every screen has its own composable wrapped by a NavGraphBuilder extension function. For a common screen be like:

fun NavGraphBuilder.VerifyCodeGraph() {
    composable(
        route = Destinations.VerifyCodeScreen().route,
    ) {
        VerifyCodeScreen()
    }
}

For managing the route of every screen, we can use a sealed class like this:

sealed class Destinations(val route: String) {
    object NewsListScreen : Destinations("news_list_screen")
    data class NewsDetailScreen(val news: String = "news") : Destinations("news_detail_screen")
    object FavoriteNewsScreen : Destinations("favorite_news_screen")
    object ProfileScreen : Destination("profile_screen")
    object SettingScreen : Destination("setting_screen")
    object ThemeScreen : Destination("theme_screen")
    object LoginScreen : Destination("login_screen")
    object VerifyCodeScreen : Destination("verify_code_screen")
    ...
}
fun NavGraphBuilder.settingListGraph(
    navController: NavController,
) {
    composable(Destinations.SettingScreen.route) {
        NewsListRoute(
            onNavigateToThemeScreen = {
                navController.navigate(
                    route = Destinations.ThemeScreen().route,
                )
            }
        )
    }
}

In this way, there is no need to depend your feature module on the navigation library. There will be the app module responsibility to handle navigation stuff.
If some screens can be grouped in a nested graph, do like this:

fun NavGraphBuilder.profileGraph(
    navController: NavHostController
) {
    navigation(
        startDestination = Destination.ProfileScreen.route,
        route = Destination.ProfileScreen.route.addGraphPostfix(),
    ) {
        composable(Destination.ProfileScreen.route) {
            ProfileScreen(
                onNavigationToLoginScreen = {
                    navController.navigate(
                        route = Destination.LoginScreen.route.addGraphPostfix(),
                    )
                }
            )
        }
        loginGraph()
    }
}

Job Offers

Job Offers


    Senior Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

,

Branching out to Jetpack Compose

As one of the most widely used social media platforms, Twitter is always hunting for ways to better connect its users. In early 2021 the Client UI team at Twitter began the task of integrating…
Watch Video

Branching out to Jetpack Compose

Nacho López & Chris Banes
Staff Software Engineer
Twitter

Branching out to Jetpack Compose

Nacho López & Chr ...
Staff Software Engin ...
Twitter

Branching out to Jetpack Compose

Nacho López & C ...
Staff Software Engineer
Twitter

Jobs

public open fun navigate(
    @IdRes resId: Int,
    args: Bundle?,
    navOptions: NavOptions?,
    navigatorExtras: Navigator.Extras?
)

‌As you can see, it gets an id for the destination. Let’s write an extension function so we can use it:

fun NavController.navigate(
    route: String,
    args: Bundle,
    navOptions: NavOptions? = null,
    navigatorExtras: Navigator.Extras? = null
) {
    val routeLink = NavDeepLinkRequest
        .Builder
        .fromUri(NavDestination.createRoute(route).toUri())
        .build()

    val deepLinkMatch = graph.matchDeepLink(routeLink)
    if (deepLinkMatch != null) {
        val destination = deepLinkMatch.destination
        val id = destination.id
        navigate(id, args, navOptions, navigatorExtras)
    } else {
        navigate(route, navOptions, navigatorExtras)
    }
}

And for using this function in the source screen:

fun NavGraphBuilder.newsListGraph(
    navController: NavController,
) {
    composable(Destinations.NewsListScreen.route) {
        NewsListScreen(
            onNavigateToDetailScreen = { news ->
                navController.navigate(
                    route = Destinations.NewsDetailScreen().route,
                    args = bundleOf(Destinations.NewsDetailScreen().news to news)
                )
            }
        )
    }
}

In the destination screen:

fun NavGraphBuilder.newsDetailScreen() {
    composable(
        route = Destinations.NewsDetailScreen().route,
    ) { entry ->
        val news = entry.parcelableData<News>(Destinations.NewsDetailScreen().news)
        NewsDetailScreen(news = news,)
    }
}


inline fun <T> NavBackStackEntry.parcelableData(key: String): T? {
    return arguments?.parcelable(key) as? T
}

This article was originally published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog

How to animate BottomSheet content using Jetpack Compose

Early this year I started a new pet project for listening to random radio…
READ MORE
blog
Yes! You heard it right. We’ll try to understand the complete OTP (one time…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu