Android Navigation Component + Login Flow + Nested BottomNavigationView

流过昼夜 提交于 2020-05-25 04:49:04

问题


I'm struggle building a Navigation, like on the image below, with the Android Navigation Component.

I need a "Login Flow", check if the user is logged in and then navigate to the main part of the app (also clearing the backstack). The main part contains a BottomNavigationView with three tabs. But if I click on a settings icon on the toolbar, I also want to display a fullscreen SettingsFragment.

Did I need two NavHostFragments with separate navigation graphs? If yes, how can I combine them and also have a proper backNavigation?

Any ideas how realize a navigation like this with the android navigation components?


回答1:


First of all you can check Conditional Navigation

Second of all, you can do it with only one nav_graph. But you need only one Activity for this. After that, you can check in it like this:

navController.addOnDestinationChangedListener { _, destination, _ ->
         if(destination.id == R.id.mainFragment){
             if(userIsLoggedIn()){
              //start LoginFragment
              //hide bottom navigation
             }else{
                //show bottom navigation
             }
          } 
        }

But your starter fragment should be MainFragment and not LoginFragment




回答2:


You can use

navController.setGraph(R.navigation.xxxx)

inside some navController.

the setGraph



来源:https://stackoverflow.com/questions/55470088/android-navigation-component-login-flow-nested-bottomnavigationview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!