首页 > 解决方案 > 返回导航控制器的状态(Android导航组件)

问题描述

我正在使用按钮导航和 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"/>

标签: androidandroid-architecture-componentsandroid-architecture-navigation

解决方案


推荐阅读