androidandroid-architecture-componentsandroid-navigationviewup-buttonandroid-architecture-navigation

Login - Navigation Architecture Component


I implemented conditional navigation to my LoginFragment, with android navigation architecture component. The problem I facing now, is that I would like to hide the up button on the toolbar, and the disable any in-app navigation while the user is not logged in.

I would like to be able to implement this with a one-activity approach, where the Activity sets up the in app navigation UI and the navController like in the android sunflower demo, and the navigation destinations are Fragments.

I implemented the conditional navigation as discribed here: Navigation Architecture Component - Login screen

How can I properly implement hiding the navigation and the up button on the login screen, with Navigation Architecture Component?


Solution

  • I don't know exactly what you mean by hiding navigation, but I will assume you mean hiding a drawer layout. To hide the up button and lock the drawer add the following to your MainActivity's onCreate. I'm using Kotlin.

    myNavController.addOnDestinationChangedListener { _, destination ->
        if (destination.id == R.id.loginFragment) {
            myDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
            myToolbar.setVisibility(View.GONE)
        } else {
            myDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
            myToolbar.setVisibility(View.VISIBLE)
        }
    

    To make just the up button go away use myToolbar.setNavigationIcon(null) and to make it come back use myToolbar.setNavigationIcon(R.id.my_icon)