Search code examples
androidandroid-fragmentsandroid-architecture-navigation

Navigation Architecture Component - BottomNavigationView with Login Screen


I have my MainActivity setup which looks like this :

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.host.HostActivity">



    <fragment
        android:id="@+id/mainNavigationFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/main_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

And the bottom navigation has 3 tabs which can only be shown after some initial authentication and setup user calls have been performed.

What is the best way to show a Loading/Setting up user screen before I let user start using the bottom nav view.

One way I am thinking is to delay the setup of NaHostFragment in my Activity until all the setup is done. So basically toggle the visibility of mainNavigationFragment and BottomNavigationView till the setup is completed.

Are there any other ideas ?


Solution

  • The NavigationUI documentation actually uses hiding a BottomNavigationView as its example. You could also use an OnDestinationChangedListener to update the visibility of your BottomNavigationView, say, only hiding it while the user is on the login screen (note that as per the Conditional Navigation documentation, you shouldn't be doing this in the start destination of your graph, but redirecting users to those screens):

    navController.addOnDestinationChangedListener { _, destination, _ ->
      if(destination.parent?.id == R.id.login) {
        bottomNavigationView.visibility = View.GONE
      } else {
        bottomNavigationView.visibility = View.VISIBLE
      }
    

    }