2

I have a main nav graph with included subgraphs to provide multiple back stacks with bottom navigation view. It works, graphs ids are consistent with the bottom menu items id. The main problems arise when I try to pop a back stack on the reselect bottom item callback.

For example, three tabs: home, search, and account. If reselect account or search, the tab is being switched to the home. If reselect the home tab (which is the first in the bottom navigation view) and try to navigate to some screen, the crash occurs. It is for navigation to some screens

java.lang.IllegalArgumentException: Navigation action{spme action} cannot be found from the current destination

and and I select some other tab is:

NullPointerException
            at androidx.navigation.ui.NavigationUI.onNavDestinationSelected(NavigationUI.kt:69)

It seems like I pop back stack in some way wrong. Here is the code of the main_graph:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_nav_graph"
    app:startDestination="@id/home_graph">
    <include app:graph="@navigation/home_graph" />
    <include app:graph="@navigation/search_graph" />
    <include app:graph="@navigation/account_graph" />
</navigation>

The menu:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/home_graph"
        android:icon="@drawable/selector_home_nav_bar"
        android:title="@string/label_home" />
    <item
        android:id="@+id/search_graph"
        android:icon="@drawable/selector_search_nav_bar"
        android:title="@string/label_search" />

    <item
        android:id="@+id/action_bookmarks"
        android:icon="@drawable/selector_bookmarks_nav_bar"
        android:title="@string/label_study_tools" />
    <item
        android:id="@+id/account_graph"
        android:icon="@drawable/selector_account_nav_bar"
        android:title="@string/label_account" />
</menu>

and the fragment with navigation

private fun setNavigation() {
    binding.bottomNavBar.itemIconTintList = null
    binding.bottomNavBar.setOnItemReselectedListener {
        navController?.popBackStack(it.itemId, false)
    }
    navController?.let { NavigationUI.setupWithNavController(binding.bottomNavBar, it) }
}
DanMan
  • 692
  • 9
  • 22

0 Answers0