Search code examples
androidshow-hidebottomnavigationview

How to show BottomNavigationBar again on navigate back


I'm using a BottomNavigationBar with HideBottomViewOnScrollBehavior to hide it when user scrolls down and display it when user scrolls up. This works great.

But how can i show the BottomNavigationBar again when it's hidden because user scrolled down and navigates back over back button?

At the moment my BottomNavigationView stays hidden.

I'm using support library 28.0.0


Solution

  • Maybe somebody has a better solution for this, but for now i came up with the following.

    In my MainActivity of my SingleActivity app i added the following function to simulate up scrolling:

    fun ensureBottomNavigation() {
        if(bottomNavigationView.translationY != 0f) {
            val layoutParams = bottomNavigationView.layoutParams as CoordinatorLayout.LayoutParams
            val behavior = layoutParams.behavior as HideBottomViewOnScrollBehavior
    
            behavior.onNestedScroll(container, bottomNavigationView, host_fragment.view!!, 0, -1, 0, 0, 0)
        }
    }
    

    In every Fragment of my app i'm calling this function in onResume() like this:

    override fun onResume() {
        super.onResume()
    
        // Ensure that bottom navigation view is visible onResume()
        (activity as MainActivity).ensureBottomNavigation()
    }