10

I am using navigation component and BottomNavigationView, i am facing a problem, that is when i go to from fragment 1>2>5>4>3 and when i press back button i get fragment 1. I know this is the default behavior but i don't want this, i want to save them in backstack so when i press back button it should go to fragment 4 not 1. I have been trying and searching but i couldn't find any solution. Can i put fragment manually into backstack in kotlin?

My code:

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
           .......................
           .......................>

            <androidx.appcompat.widget.Toolbar
               ................
               ................/>

            <fragment
                android:id="@+id/app_nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:navGraph="@navigation/app_nav"
                ..............
                ............../>
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/app_bottom_nav_view"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/white_grey_border_bottom"
                app:menu="@menu/bottom_nav_menu"
                app:labelVisibilityMode="unlabeled"
                ...........
                .........../>

</androidx.constraintlayout.widget.ConstraintLayout>

app_nav.xml

<navigation 
    ...........
    ...........
    android:id="@+id/app_nav"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.instagram_clone.ui.HomeFragment"
        android:label="HomeFragment"
        tools:layout="@layout/fragment_home"/>
    .............
    .............
    .............
    .............
</navigation>

MainActivity.kt

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)

        val binding  = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        val bottomNavView = binding.appBottomNavView
        val navController = findNavController(R.id.app_nav_host_fragment)

        bottomNavView.setupWithNavController(navController)

    }
}
Askani
  • 424
  • 7
  • 19

1 Answers1

21

As per the documentation on onNavDestinationSelected() (the method that setupWithNavController() uses when selecting a MenuItem):

By default, the back stack will be popped back to the navigation graph's start destination. Menu items that have android:menuCategory="secondary" will not pop the back stack.

So just add android:menuCategory="secondary" to each of the menu items used with your BottomNavigationView.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • And how to not put a fragment again into back stack that is already in it? – Askani Oct 07 '19 at 14:42
  • 1
    You'd need to set your own `OnNavigationItemSelectedListener` after the call to `setupWithNavController` and do your own `navigate()` call, adding the `NavOptions` object with `setPopUpTo` the same destination you're navigating to, following the same pattern that [the code behind onNavDestinationSelected() does](https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/navigation/navigation-ui/src/main/java/androidx/navigation/ui/NavigationUI.java#57) - there's no built in helper for that behavior. – ianhanniballake Oct 07 '19 at 19:07
  • 2
    I'm currently having the issue that nothing is popped. (with default setup) If i navigate 1 > 2 > 3 and then press back, i get to 2. (on 2.1.0). – cwiesner Dec 07 '19 at 18:02
  • 2
    @cwiesner - sounds like you popped the start destination of your graph. You shouldn't do that (see the Principles of Navigation documentation and the docs there on Conditional Navigation) – ianhanniballake Dec 07 '19 at 18:43
  • oh thanks for the hint. I was using an onboarding fragment as start destination which called then the first destination in the bottom nav. will need to turn that order around. – cwiesner Dec 08 '19 at 12:43
  • 4
    @ianhanniballake my start destination is splash screen and I navigate from splash to this screen with bottomNavigation. so if i do not popup my start destination, splashh screen will be always there. so what is the best practice to handle this problem? – Saman Sattari Jun 11 '21 at 21:15
  • 1
    @Askani here is how to not put a fragment again into back stack that is already in it: https://stackoverflow.com/a/68779398/2425851 – NickUnuchek Nov 26 '21 at 13:29
  • @SamanSattari I'm having the same problem as you. Were you able to figure out a way to solve the issue? – heisenberg91 Mar 10 '23 at 16:32