Возвращает состояние контроллера навигации (компонент навигации Android) - PullRequest
0 голосов
/ 01 марта 2019

Я использую кнопку навигации и NavController.Все мои фрагменты в одном навигационном контейнере.Когда я переключаюсь между вкладками, я хочу сохранить стек путешествий, который позволяет вернуться к текущему состоянию вкладок.Я пытаюсь использовать NavController.saveState () и после restoreState ().Но после вызова этой функции ничего не изменилось.Как мне этого добиться?

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"/>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...