Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Jetpack Compose: change bottom nav back press behavior with Navigation Components

Posted on November 11, 2022 By

Solution 1 :

I cannot answer your first question, but for your second one: I just stumbled upon BackHandler. I just tried it real quick and it got called instead of closing the app, so that should solve your issue.

Problem :

I finally started using Compose for Android and I am currently stuck at navigation. I have two questions:

1.
I have implemented a bottom navigation with three buttons and one of the buttons triggeres a nested navigation graph, like:

 HOME  ---  CONTACTS  ---  FAVOURITES 
   |            
DETAILS 

So when I open details screen from home and switch tabs, the behavior is correct, the state and last destination in the nested graph is memorized and when I tap again on home, it keeps the state and switches back to details. However, if I am on any of the other tabs and press back, it brings me to home but it resets the state, it doesn’t bring me back into details. How can I achieve that?

Navigation Bar:

@Composable
fun BottomNavigationBar(navController: NavHostController) {
BottomNavigation {
    val backStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = backStackEntry?.destination?.route

    BottomNavigationCollection.items.forEach { navItem ->
        BottomNavigationItem(
            selected = currentRoute == navItem.route,
            onClick = {
                navController.navigate(navItem.route) {
                    popUpTo(navController.graph.findStartDestination().id) {
                        saveState = true
                    }
                    launchSingleTop = true
                    restoreState = true
                }
            },
            icon = {
                Icon(
                    imageVector = navItem.icon,
                    contentDescription = navItem.title
                )
            },
            label = {
                Text(text = navItem.title)
            },
        )
    }
}

Navigation Host:

@Composable
fun NavigationHost(navController: NavHostController) {
NavHost(
    navController = navController,
    startDestination = NavigationRoutes.Home.route,
    route = "root"
) {
    homeGraph(navController)

    composable(NavigationRoutes.Contacts.route) {
        ContactsScreen()
    }

    composable(NavigationRoutes.Favorites.route) {
        FavouritesScreen()
    }
}

}

Nested Home Navigation Graph:

fun NavGraphBuilder.homeGraph(navController: NavController) {
navigation(
    startDestination = NavigationRoutes.Dashboard.route,
    route = NavigationRoutes.Home.route
) {
    composable(NavigationRoutes.Dashboard.route) {
        DashboardScreen(navController)
    }

    composable(NavigationRoutes.Details.route) {
        DetailsScreen(navController)
    }
}

}

2.
When I am back in home with the entire navigation stack clear and press back once more, naturally the app closes. How can I intercept back press and implement a “tap again to exit” confirmation feature at that point?

READ  [FIXED] how to write wifi config to nfc tag using android studio and java?
Powered by Inline Related Posts
Android Tags:android, android-jetpack-compose, android-navigation, kotlin

Post navigation

Previous Post: [FIXED] android – How to detect when third party app’s dialog box appears in React native?
Next Post: [FIXED] android – Get the list of user SIM cards and select it – flutter

Related Posts

[FIXED] “Attempt to invoke interface method ‘voidorg.unimodules…’ on null object reference” error message with a React-Native app on an android emulator Android
[FIXED] kotlin – Does Android force a destroy of a Service after 1 minute if the controlling application is running in background? Android
[FIXED] HTTP GET-Request in Kotlin/Android Studio Android
[FIXED] android – Firebase – How do I insert a new child into my Users node on click? Android
[FIXED] intellij idea – Where does Android Studio show what `adb` is doing during launch? Android
[FIXED] android – How to read multiple Bar Code from Newland MT90? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme