简体   繁体   English

Jetpack Compose 底部导航、fab 按钮和保存屏幕 state

[英]Jetpack Compose bottom navigation, fab button and save screen state

i have been playing with jetpack compose, bottom navigation and fab button.我一直在玩 jetpack compose、底部导航和 fab 按钮。

The expected behavior of the application is as follows (bottom nav a/b/c):应用程序的预期行为如下(底部导航 a/b/c):

a) Screen 1 --> (+ fab) --> Screen 1.A a) 屏幕 1 --> (+ fab) --> 屏幕 1.A

b) Screen 2 --> (+ fab) --> Screen 1.B b) 屏幕 2 --> (+ fab) --> 屏幕 1.B

c) Screen 3 c) 屏幕 3

When i am in screen 1, and press + button, it will go to screen 1.A当我在屏幕 1 中并按 + 按钮时,它将 go 到屏幕 1.A

Then, if i press bottom bar button b, it will show screen 2. At this state, if i press system back button, it will go back to Screen 1 instead of screen 1.A, which was the last screen.然后,如果我按下底部栏按钮 b,它将显示屏幕 2。在此 state,如果我按下系统返回按钮,它将 go 返回屏幕 1 而不是屏幕 1.A,这是最后一个屏幕。

Also, if i am in screen 1.A, and then in screen 2 press + to go to screen 2.B, if i press button A in the nav bar, it will show Screen 1 instead of screen 1.A, which was the last screen of that button sequence.另外,如果我在屏幕 1.A 中,然后在屏幕 2 中按 + 到 go 到屏幕 2.B,如果我在导航栏中按按钮 A,它将显示屏幕 1 而不是屏幕 1.A,这是该按钮序列的最后一个屏幕。

In any screen, if i press system back button, it will go to screen1 and then exit the app, for example i am in screen 1.B, press system back, and will go directly to Screen 1, instead of Screen 2.在任何屏幕,如果我按系统返回按钮,它会 go 到屏幕 1 然后退出应用程序,例如我在屏幕 1.B,按系统返回,go 将直接到屏幕 1,而不是屏幕 2。

Here is my code这是我的代码

Bottom nav bar:底部导航栏:

BottomNavigation(
        backgroundColor = Primary
    ) {
       items.forEach{ screen ->
           BottomNavigationItem(
               icon = { Icon(imageVector = screen.icon, contentDescription = screen.route, tint = Color.White) },
               selected = currentDestination?.route == screen.route,
               onClick = {
                   navController.navigate(screen.route) {
                       popUpTo(navController.graph.findStartDestination().id) {
                           saveState = true
                       }

                       launchSingleTop = true
                       restoreState = true
                   }
               }

           )
       }
   }

Fab button:按钮:

fabNav = "screen_1.b" (or screen_1.a, there is a logic before)

FloatingActionButton(
                    onClick = {
                        navController.navigate(fabNav) {
                            popUpTo(navController.graph.findStartDestination().id) {
                                saveState = true
                            }
                            launchSingleTop = true
                            restoreState = true
                        }
                    },
                    backgroundColor = Color.Gray,
                    contentColor = Color.White
                ) {
                    Icon(Icons.Filled.Add, "")
                }

Navigation host:导航主机:

fun NavigationHost(navController: NavHostController){
    
    NavHost(navController = navController, startDestination = Screen.Home.route) {
        composable(Screen.Screen1.route){
            Screen1()
        }
        composable(Screen.Screen2.route){
            Screen2()
        }

        composable(Screen.Screen3.route){
            Screen3()
        }

        composable(Screen.AddToScreen1.route){
            AddToScreen1()
        }
        composable(Screen.AddToScreen2.route){
            AddToScreen2()
        }

    }
}

Thanks!谢谢!

Just remove the popUpto("") from your navigation compose, because it will remove the screens from the back stack.只需从导航组合中删除popUpto("") ,因为它会从后台堆栈中删除屏幕。

Just use只需使用

navController.navigate(screen.route)

and

navController.navigate(fabNav)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM