Skip to content
Advertisement

how to use Navigation component navhostfragment while altering full screen/above bottom Navigation

I’m using the single activity multi fragments with navigation component.how do i hide the bottom navigation bar for some of the fragments?

i tried the following:

  1. controlling the visibility of the bottomnavigation bar through databinding.(buggy)
  2. toggling the bottomnavigation visibility before opening the fragment and on the backstack ( buggy)
  3. making 2 host fragments: 1 full screen, 1 bound by the bottomnavigation
  4. making 2 navgraphs ..

activity_main.xml:

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:visibility="@{viewModel.uiUtils.shouldShow ? View.VISIBLE:View.GONE}"/>

mainactivity.java:

    private void observeShouldShow() {
        mainViewModel.uiUtils.getShouldShow().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                ViewGroup.LayoutParams layoutParams = binding.bottomNavigation.getLayoutParams();
                if (mainViewModel.getUiUtils().getShouldShow().getValue()) {
                    binding.bottomNavigation.setVisibility(View.VISIBLE);

                    layoutParams.height = 170;
                    binding.bottomNavigation.setLayoutParams(layoutParams);
                } else {
                    layoutParams.height = 0;
                    binding.bottomNavigation.setLayoutParams(layoutParams);

                 binding.bottomNavigation.setVisibility(View.INVISIBLE);
                }
            }
        });

the bottomnavbar blinks when switching between full screen fragments and normal fragments

Advertisement

Answer

i used OnDestinationChangedListener, as @Lavepe answered … sorry didnt check here for a long time here is my code:

                if (destinationLabel.equals("FragmentX")) {
                    showBottomNav(true);
                    badgeBehaviour(false, false);}

the ui function:

private void showBottomNav(boolean b) {
    binding.bottomNavigation.setVisibility(b ? View.VISIBLE : View.GONE);
}

the view above it is :

    <fragment
        android:id="@+id/navHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/viewcartbadge"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

Best Regards Hope You Find it Useful

Advertisement