17

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?

enter image description here

D.Roters
  • 221
  • 2
  • 6
  • Have you had any idea about it? I am trying to do it with mainFragment as start destination, here I validate session active, if not, navigate to login, with popUp="nav_graph" and popUpToInclusive="false" and seems to work fine, using your provided current navigation graph flow. With destination listener I'm hiding toolbar and bootm navigation when detects loginFragment . I'm using version 2.0.0-rc02 – wilmerlpr Oct 30 '19 at 06:58
  • Curious how did you get the bottomnavview in the fragment working with nav graph ? – duskandawn Jan 14 '20 at 00:12
  • @duskandawn did it work for you? I am having problems implementing it – Zeeshan Shabbir Jun 27 '20 at 15:01

2 Answers2

7

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

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
0

You can use

navController.setGraph(R.navigation.xxxx)

inside some navController.

the setGraph

N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32