I'm currently using navigation version 2.5.2
I've got BottomNavigationView for main menu.
For example,
Main Menu : A, B, C
A(home) can go A -> Aa -> Ab -> D
B can go B -> D
C can go C -> Ca -> D
something like that.
Here's what I have to do,
1. When user click bottom menu, the navigation stack must erase. (ex. When user go A->Aa, and then click B menu at the bottom and back to A, Aa should be erased. User is to see A not Aa)
2. When user click system back button, the stack should be erased one by one. (ex. A->Aa->Ab. When click back, Ab gone and Aa gone and A)
The problem is, When I go B->D and click back(B appeared) and back again(A appeared as it's home). And!!! click B again,
It doesn't even created at the first click, and it is shown from the second click.
No error msg.
<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/mobile_navigation"
app:startDestination="@id/home">
<include app:graph="@navigation/home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.www.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard">
<action
android:id="@+id/action_dashboard"
app:destination="@+id/navigation_seconddash"
app:popUpTo="@id/navigation_dashboard"
app:popUpToSaveState="true" />
</fragment>
<fragment
android:id="@+id/navigation_seconddash"
android:name="com.example.www.ui.home.SecondDashboardFragment"
android:label="DashBoard2"
tools:layout="@layout/fragment_seconddashboard"/>
<fragment
android:id="@+id/navigation_notifications"
android:name="com.example.www.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
</navigation>
Dashboard clickevent
binding.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_dashboard);
}
});
It works find when click menu instead of back button.
https://www.youtube.com/shorts/RgyjKWDLx0I
Above is video I recorded cos hard to explain in text.
I've tried deeplink, but it doesn't clear stack. tried different versions, doesn't work.
When you use app:popUpTo="@id/navigation_dashboard", it will delete the dashboard and jumps one back from it, in your case to home. Just delete those 2 lines to avoid that situation.
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.www.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard">
<action
android:id="@+id/action_dashboard"
app:destination="@+id/navigation_seconddash" />
</fragment>
And for your 2 problems add below line, it will delete your backstack when you click any of the bottom nav items, and the deleting one by one situation works well:
yourBottomNavView.setOnItemSelectedListener { item ->
NavigationUI.onNavDestinationSelected(item, yourNavController)
yourNavController.popBackStack(item.itemId, inclusive = false)
true
}