Android Navigation Component + Login Flow + Nested BottomNavigationView Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android Navigation Component + Login Flow + Nested BottomNavigationView without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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

Answer

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

We are here to answer your question about Android Navigation Component + Login Flow + Nested BottomNavigationView - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji