android - 返回导航控制器的状态(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"/>
解决方案
推荐阅读
- php - var_dump() 在 HTTP POST 上不输出任何内容
- java - Powershell 脚本在满足条件后未退出循环并被捕获失败条件
- elasticsearch - 弹性搜索将我上传的其他网址链接变成了我上传的其他网址
- javascript - Selenium/Xpath 选择包含文本的选项
- python - 为什么在 Keras 中创建堆叠合奏时出现 Graph disconnected value 错误?
- python - Django DRF 和泛型关系:如何从 API 响应中获取 content_object 字段?
- javascript - JS:如何将获取的数据保存到变量中?
- openssl - 使用 OpenSSL 从 EVP_PKEY 中提取 AlgorithmIdentifier (OID)
- node.js - 如何根据节点JS中的API请求检索用户的机器IP地址?
- swift - 在 GMSMapView 中 infoWindowTap 之后调用视图