0

I have a problem when using BottomNavigation + Navigation components. Basically when navigates to fragment via bottom navigation, popBackStack() brings the app to the startDestination instead previous fragment.

class HomeFragment {
...
    bottomNavigation.setupWithNavController(findNavController(R.id.navHost))
...
}
<navigation 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:id="@+id/main_navigation"
    app:startDestination="@id/splashFragment">

    <fragment
        android:id="@+id/splashFragment"
        android:name="SplashFragment"
        android:label="SplashFragment"
        tools:layout="@layout/fragment_splash">
        <action
            android:id="@+id/action_splashFragment_to_A"
            app:destination="@id/A" />
    </fragment>

    <fragment
        android:id="@+id/A"
        android:name="AFragment"
        android:label="AFragment"
        tools:layout="@layout/A">
        <action
            android:id="@+id/action_A_to_B"
            app:destination="@id/B" />
    </fragment>

    <fragment
        android:id="@+id/B"
        android:name="BFragment"
        android:label="BFragment"
        tools:layout="@layout/B">
        <action
            android:id="@+id/actiob_B_to_homeFragment"
            app:destination="@id/homeFragment" /> <!--bottomNavigation implemented in this fragment-->
    </fragment>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/item1" />
    <item
        android:id="@+id/item2" />
    <item
        android:id="@+id/item3" />
</menu>

When I navigate to any on those item framgment from bottom navigate then firing findNavController().popBackStack() brings the app back to SplashFragment and should to HomeFragment, as there is bottom navigation implemented.

P.Juni
  • 2,365
  • 14
  • 26

2 Answers2

0

You can use popBackStack(int destinationId, boolean inclusive) with these two parameters. in this way, navController attempts to pop the controller's back stack back to a specific destination.

see this link.

Also you have another option for popping the backstack of your navigation.

see this link.

BlueMeth
  • 1
  • 2
  • thanks for the answer but `findNavController().popBackStack(R.id.homeFragment, true)` not working meaning the fragment doesnt go anywhere, and the `app:popUpTo="@+id/homeFragment" app:popUpToInclusive="true"` also brings back to `SplashScreen` – P.Juni Mar 05 '20 at 16:10
0

Set your startDestination as home in Splash's onDestroyView or in Home's onCreate

findNavController().graph.startDestination = R.id.homeFragment

faskN
  • 704
  • 5
  • 4