https://kotlinlang.org logo
Join Slack
Powered by
# navigation-architecture-component
  • t

    Tepes Lucian Victor

    09/03/2021, 4:11 PM
    I’ve started a new project using navigation component multiple backstack support and i’ve hit an issue with having an auth subgraph
    Copy code
    <?xml version="1.0" encoding="utf-8"?>
    <navigation xmlns:android="<http://schemas.android.com/apk/res/android>"
        xmlns:app="<http://schemas.android.com/apk/res-auto>"
        android:id="@+id/main"
        app:startDestination="@id/graph1">
    
        <include app:graph="@navigation/graph1" />
        <include app:graph="@navigation/graph2" />
        <include app:graph="@navigation/graph3" />
    
        <include app:graph="@navigation/auth" />
    
        <action
            android:id="@+id/action_to_auth"
            app:destination="@id/auth"
            app:popUpTo="@id/graph1"
            app:popUpToInclusive="true"
            app:launchSingleTop="true" />
    
        <action
            android:id="@+id/action_to_main"
            app:destination="@id/graph1"
            app:popUpTo="@id/auth"
            app:popUpToInclusive="true"
            app:launchSingleTop="true" />
    </navigation>
    When starting the app if the user is logged out i redirect to auth flow using the `action_to_auth`and when he logs out, i’m calling
    navigate(R.id.action_to_auth)
    . The issue with multiple backstacks is that saved stacks aren’t cleared and there is no way to query what back stacks were previously saved into the fragment manager.
  • a

    Apex

    09/10/2021, 5:14 PM
    Does anyone have a good sample for handling User login/ register with Nav component? The docs recommend having a fixed start destination and applying authentication on pages that need it
  • a

    Apex

    09/10/2021, 5:15 PM
    But I don't want user to access any part of the app without authentication
    t
    • 2
    • 3
  • v

    Vivekpanchal64

    11/30/2021, 5:06 AM
    i have a login graph where once user logged in i navigate him to home screen. • but when he skip login and move to home screen and tries any user related action i want him to go back to login but once login i want him to stay at the page he was looking into. can anyone help me out how should i do this ?
  • c

    Cristina Uroz

    05/29/2022, 1:47 PM
    Hello everyone! A question relate to compose navigation: has anyone had trouble with navigation on a Xiaomi phone? I have a MIUI 13 that doesn’t complete the start navigation flow until you touch the screen
  • d

    Dhaval Gondaliya

    09/23/2022, 11:24 AM
    anyone know how to use
    findNavController()
    in
    Application
    class?
    • 1
    • 1
  • r

    Rafael Costa

    10/02/2022, 9:13 PM
    Hi all 👋 Could someone please explain to me how it is possible that ViewModels are not cleared on multi back stack scenario, since the corresponding NavBackStackEntries are popped from back stack, I really thought that would be the case, but I was surprised to find it isn't. I thought the state restoration would rely on the SavedStateHandle bundle to do it's thing, but seems like that's not the case, VMs are not even cleared.
  • w

    why

    04/12/2023, 1:54 PM
    Hello there. Is it possible to save the state of a nested navigation without popping up to start destination? I'm trying to implement a multi-stack navigation across bottomBar tabs. (navigation in Compose)
    Copy code
    navOptions {
                    popUpTo(navController.graph.findStartDestination().id) {
                        saveState = true
                    }
                    restoreState = true
                }
  • v

    Vivekpanchal64

    05/23/2023, 7:10 PM
    Hey Can someone guide me how to architect my base fragment to support to maintaining the view If user have came back from normal back press And if user done some action then comeback refresh the page and dont retain the view . Have anyone structured the same in baseFragment using databinding ??
  • g

    galex

    08/09/2023, 12:21 PM
    Hello, I've got Fragment A (Start Destination) and then Fragment B which both use the same ViewModel, both using
    saveStateHandle["id"]
    to get an id from a deeplink. When coming back from Fragment B to Fragment A, The ViewModel of Fragment A also receives the same parameter
    id
    as the ViewModel of Fragment B receives it. Weird or what am I missing here?
    d
    • 2
    • 2
  • n

    Nacho Ruiz Martin

    10/09/2023, 3:47 PM
    Hey 👋 After updating some libraries, when invoking
    onBackPressed
    on our Activities, navigation component’s
    popBackStack
    is not called, only
    finish
    , even if the graph is not on its root. Is this expected? Should we override the default back behaviour with
    OnBackPressedDispatcher
    to manually call
    popBackStack
    and
    finish
    when the backstack is cleared out?
  • y

    Yasutaka Kawamoto

    03/13/2024, 12:52 AM
    Hello, I'm using androidx.navigation:navigation-*:2.7.5 and noticed a potential issue with NavController#onGraphCreated not being called when calling setGraph twice. This seems to be related to the commit found here: https://android.googlesource.com/platform/frameworks/support/+/d22366d59a46aa16aa79d1d691cd5cb55f56c378 I'm trying to find a way to force NavController#onGraphCreated to be called in this scenario. Here's why I need this: I have an Activity with launchMode set to singleTop. • When the Activity is started for the first time, onCreate is called and NavController#setGraph is used to set the initial graph. • When the Activity is started again (via startActivity), onNewIntent is called. Inside onNewIntent, I call NavController#setGraph again to update the UI. This worked fine in 2.7.4, but in 2.7.5, the UI no longer updates when calling setGraph in onNewIntent. Is there any way to force NavController#onGraphCreated to be called in this situation? Any help would be greatly appreciated.
    d
    • 2
    • 2
  • a

    agrosner

    05/14/2024, 3:50 PM
    hey, for jetpack compose navigation, does anyone know when we dispatch multiple navigational events in a row, will the intermediate routes get rendered?:
    Copy code
    val navController = rememberNavController()
    navController.navigate(RouteA)
    navController.navigate(RouteB)
    will
    RouteA
    compose, or only compose when we go back from
    RouteB
    ?
    • 1
    • 1
  • n

    Natasha Jordanovska

    06/04/2024, 6:54 AM
    Hello everyone! I’m evaluating different strategies for implementing conditional navigation in a Jetpack Compose application and would love to hear your thoughts on the best practices. Here are three approaches I’m considering: 1. Centralized Navigation Logic with LaunchedEffect: This method involves managing a
    nextScreen
    state in the ViewModel, observed in the UI layer through a LaunchedEffect. Navigation commands are triggered in response to state changes, ensuring all navigation logic is centralized. 2. Callbacks in ViewModel: In this approach, navigation is triggered directly from ViewModel functions based on API call results or other conditions, using callbacks. This method avoids passing navigation logic to the UI and aligns with practices demonstrated in the JetSurvey app from the official Jetpack Compose samples, which can be found here. The Composables also define their own callbacks for navigation, ensuring a clean separation of concerns. 3. Using LiveData with Observers in Composables: Adapting a traditional approach where the ViewModel updates LiveData based on conditions, and Composables observe these changes to handle navigation. Although this is more common in architectures involving fragments, it could be adapted for Composables as detailed in the Android developer guide here. I’m particularly interested in scalability, testability, and ease of maintenance. Which approach do you prefer for conditional navigation in your projects, especially when using Composables, and why?
  • a

    Alvin Dizon

    07/08/2024, 11:54 PM
    Is Navigation 2.8.0 going to be released soon? I'm seeing crashes similar to https://issuetracker.google.com/issues/279644470 and it seems 2.8.0alpha01 and later versions has the fix, but would rather use a stable release if it's going to available soon.
    👀 1
    c
    • 2
    • 1
  • a

    agrosner

    08/05/2024, 4:28 PM
    hey using the
    AndroidFragment
    composable, it changes how fragments exist in the fragment manager of the host fragment. in this case, im hosting a
    NavHost
    within a fragment, where in the traditional version, same except using regular fragment transactions. When an
    AndroidFragment
    goes onto the backstack in navigation compose,
    onDispose
    is called, removing the fragment from the fragment manager. This changes how fragments typically exist in the pure fragment world, where they exist on the backstack with same instance. for legacy reasons, we have a bunch of screens expecting the same instance is used when fragment is reentered (setting fields improperly). In short, my guess is that this is expected behavior and that we should roll out our own variant of
    AndroidFragment
    emulating this behavior?
    • 1
    • 4
  • i

    Iman Sadrian

    09/11/2024, 12:56 PM
    Hello folks! I just posted my question here. It’s about finding a way to sync our DeepLinks in navgraph.xml with our DeepLink generator in Kotlin. I would appreciate any hints or if you could share your experience on this. Thanks! https://stackoverflow.com/questions/78973663/how-to-synchronize-deeplinks-between-navgraph-xml-and-my-kotlin-deeplink-generat
  • e

    Eduardo Ruesta

    09/11/2024, 3:35 PM
    Hey guys! i've been working on a Compose Multiplatform app. I used Voyager library for the navigation and everything works fine! but i wanted to move to navigation Compose. I did the migration, in Android works fine but on iOS all the lazyColumn and lazyRow for some reason feel rough when scrolling. Has anyone had something similar? I can't find what the problem could be. Thanks
  • d

    dave08

    09/15/2024, 11:15 AM
    Hi! Is there a way to make/use a generic CustomNavType that just serializes the content to Json using Kotlinx serialization and puts it in the bundle (I'm using the new type-safe navigation in 2.8.0...)?
    • 1
    • 1
  • d

    dave08

    09/15/2024, 11:17 AM
    Also, how to I test if I'm in a certain route with the type-safe nav api? I don't know the url of the route anymore... (unless I log it and hard code it's string 😵‍💫?)
  • a

    Andrey Nikanorov

    09/18/2024, 9:13 AM
    Hello, why NavController saves state only if it doesn't already exist? How it possible to replace the saved state, for example to update with the new deeplink params? https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]time/src/main/java/androidx/navigation/NavController.kt;l=739
  • a

    agrosner

    09/23/2024, 6:26 PM
    hey is there a good way not using restricted API to turn a typesafe Route into a full url string? Ive written up this function, but it is using internal APIs:
    Copy code
    /**
     * Generates a full route URL for a destination.
     *
     * This utilizes an internal jetpack compose typesafe navigation API.
     */
    @SuppressLint("RestrictedApi")
    fun Routable.fullRoute(destination: NavDestination): String {
        val args = destination.arguments.mapValues { it.value.type }
        val routeUrl = generateRouteWithArgs(this, args)
        return routeUrl
    }
    (Routable is our own restriction on route class instances) The use case is for tracking purposes id like to record its url value
  • v

    Vinicius Matheus

    09/24/2024, 2:15 PM
    Has anyone tried the
    navigation-fragment-compose
    ? I’m having an obfuscation problem. I tried to allow the generated class on the proguard, but I haven’t had any success yet. 😕
  • a

    agrosner

    11/22/2024, 3:50 PM
    anyone know why this is restricted, and not just public?
    Copy code
    // this restricted public is needed so that the public reified [popUpTo] can call
        // private popUpToRouteClass setter
        @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
        public fun <T : Any> popUpTo(klass: KClass<T>, popUpToBuilder: PopUpToBuilder.() -> Unit) {
    we have wrapper apis that we’d like to pass kclass vs recreating the route instance when popping current screen off the navigation stack. is there a better approach?
  • s

    stevie

    12/23/2024, 2:16 PM
    Hi, do we need anything else to set up Navigation typesafe? I am doing this:
    Copy code
    @Serializable
    data object HomeRoute // route to Main screen
    
    fun NavController.navigateToHome() = navigate(route = HomeRoute) {
        popUpTo(graph.id)
    }
    
    fun NavGraphBuilder.homeScreen() {
        composable<HomeRoute> {
            HomeScreen()
        }
    }
    But got this error when navigating back from another app:
    Copy code
    android.os.BadParcelableException: Parcelable encountered IOException writing serializable object (name = java.util.LinkedHashSet)
    ....
    Caused by: java.io.NotSerializableException: xxx.navigation.HomeRoute
  • t

    tylerwilson

    01/02/2025, 5:53 PM
    I am attempting to update from using XML based BottomNavigationView to a Compose NavigationView/NavHost, and I use XML navigation graphs extensively. Most of my current Fragments use findNavController().navigate etc. How do I construct a NavHost/NavController on the Compose side (I am trying NavigationSuiteScaffold) and then pass it down to the AndroidFragment wrapped Fragments? Thank you!
    a
    • 2
    • 3
  • j

    Joost Klitsie

    03/07/2025, 9:42 AM
    Hello people! 🙂 I am using Jetpack Compose and navigation component, I was wondering: can we pass a type safe argument to a nested navigation graph? In theory,
    navigation<MyGraph> {}
    supports the same type safe objects. I was hoping we can simply access the back stack entry for that and then read out the data we passed (more in the comment). Is this possible?
    ✅ 1
    • 1
    • 5
  • e

    enighma

    05/14/2025, 9:08 PM
    Is there a recommendation or best practice around using a Scaffold around the Navhost or instead vice versa and instead have one scaffold per screen? The former makes sense to me, but it makes it messy to communicate changes to let's say the top appbar. Do either of the example compose apps showcase this? I think the jetnews app uses a scaffold per screen.
  • t

    Tomáš Procházka

    05/19/2025, 11:54 PM
    I need to have custom compose
    NavHostController
    , I need to override
    handleDeepLink
    with custom implementation. Basically I need just provide a custom source of deep link intent, it is by default take from activity. I found no other way how to do it.
    NavHostController
    and even
    handleDeepLink
    is open so it should be easy to do it. But sadly it is not, because of
    rememberNavController()
    There is custom logic to create a instance of
    NavHostController
    which I need to reimplement too. There is this:
    Copy code
    private fun createNavController(context: Context) =
        NavHostController(context).apply {
            navigatorProvider.addNavigator(ComposeNavGraphNavigator(navigatorProvider))
            navigatorProvider.addNavigator(ComposeNavigator())
            navigatorProvider.addNavigator(DialogNavigator())
        }
    And sadly
    ComposeNavGraphNavigator
    is internal, so I can't create a own instance and use it. Nothing else is internal, just this one class. Why?
  • s

    Seren

    09/04/2025, 2:34 PM
    Hello, I'm using navigation 2.9.0 in compose and I have an issue with _"intermediate conditional navigation and transitions"_; I'm navigating to an intermediate destination that contains a LaunchedEffect that chooses how to forward to the final destination.
    Home -> Intemediate -> Detail 1 or Detail2.
    After navigating we then pop this intermediate destination from the backstack. The problem is that using this technique the exitTransition no longer works when hitting the back button; the screen completes immediately with no transition. I noticed that the exitTransition works in versions 2.7.* however this version has it's own bugs (specifically on the entry transition) so I cannot revert to this version. Has anyone come across this? The only work around I can see right now is to declare a backHandler on
    Detail1 / Detail 2
    and navigating to Home with a popupTo Home argument. However its not desirable because it re-loads the screen and using launchSingleTop also removes the transition.
    a
    • 2
    • 3