1

I'm using button navigation and NavController. All my fragments in same navigation container. When I switch between tabs I want to store travel stack, that allow to return on current tab state. I try to use NavController.saveState() and after restoreState(). But after calling this function nothing changed. How I can achieve this?

val bottomNavigationListener = object :
    BottomNavigationView.OnNavigationItemSelectedListener {
    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_statistic -> handleTabClick(item.itemId, R.id.statisticsFragment)
            R.id.menu_tracker -> handleTabClick(item.itemId, R.id.trackerFragment)
            R.id.menu_wiki -> handleTabClick(item.itemId, R.id.wikiFragment)
            else -> return false
        }

        return true
    }
}



private fun handleTabClick(tabId: Int, hostFragmentId: Int) {
    if(currentTabId != tabId) {
        if(navController.currentDestination?.id != hostFragmentId) {
            destinationsHistory[currentTabId] = navController.saveState() ?: Bundle()
        } else {
            destinationsHistory[currentTabId] = Bundle()
        }

        val bundle = destinationsHistory[tabId] ?: Bundle()

        if(!bundle.isEmpty) {
            navController.restoreState(bundle)
        } else {
            navController.navigate(hostFragmentId)
        }
    } else {
        destinationsHistory[tabId] = Bundle()
        if (navController.currentDestination?.id != hostFragmentId) {
            navController.navigate(hostFragmentId)
        }
    }

    currentTabId = tabId
}


<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_graph"
        app:startDestination="@id/wikiFragment">

<fragment android:id="@+id/statisticsFragment" android:name="com.sc.overhub.view.fragment.StatisticsFragment"
          android:label="StatisticsFragment"/>
<fragment android:id="@+id/trackerFragment" android:name="com.sc.overhub.view.fragment.TrackerFragment"
          android:label="TrackerFragment"/>

<fragment android:id="@+id/wikiFragment" android:name="com.sc.overhub.view.fragment.wiki.WikiFragment"
          android:label="fragment_wiki" tools:layout="@layout/fragment_wiki">
    <action android:id="@+id/action_wikiFragment_to_wikiHeroesListFragment"
            app:destination="@id/wikiHeroesListFragment"/>
    <action android:id="@+id/action_wikiFragment_to_mapsFragment" app:destination="@id/mapsFragment"/>
</fragment>
<fragment android:id="@+id/wikiHeroesListFragment"
          android:name="com.sc.overhub.view.fragment.wiki.herosList.WikiHeroesListFragment"
          android:label="WikiHeroesListFragment">
    <action android:id="@+id/action_wikiHeroesListFragment_to_wikiHeroFragment"
            app:destination="@id/wikiHeroFragment"/>
</fragment>
<fragment android:id="@+id/wikiHeroFragment"
          android:name="com.sc.overhub.view.fragment.wiki.herosList.hero.WikiHeroFragment"
          android:label="fragment_view_hero" tools:layout="@layout/fragment_wiki_hero"/>
<fragment android:id="@+id/mapsFragment" android:name="com.sc.overhub.view.fragment.wiki.maps.MapsFragment"
          android:label="MapsFragment"/>

user294076
  • 93
  • 10

0 Answers0