android - 导航拱组件和碎片娱乐
问题描述
我BottomNavigationView
一起使用NavHostFragment
,目前只有 2 个选项卡,位于我使用的导航图下方
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_navigation"
app:startDestination="@id/fragment1">
<fragment
android:id="@+id/fragment1"
android:name="something.ListingFragment"
android:label="@string/hello" />
<fragment
android:id="@+id/fragment2"
android:name="something.ListingFragment2"
android:label="@string/Hi" />
</navigation>
而底部导航菜单是:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@id/fragment1"
android:icon="@drawable/ic1"
android:title="@string/hello"
tools:showAsAction="ifRoom" />
<item
android:id="@id/fragment2"
android:icon="@drawable/ic2"
android:title="@string/hi"
tools:showAsAction="ifRoom" />
所有设置一起使用:
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.navigation_host_fragment) as NavHostFragment
NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.navController)
问题是,每次我在选项卡之间切换时,它都会创建一个我单击的选项卡片段的新实例,如果内存中已经存在新项目,是否有办法管理它以防止创建新项目?
解决方案
推荐阅读
- r - 从 SoilGrid250m 下载 tiff 文件
- html - 使用蒙版部分更改图像颜色
- dart - 获得比动态更具体的泛型类型
- ffmpeg - 使用 ffmpeg “重新计时” RTSP 流?
- spring-cloud-contract - 消费者合约需要在 Spring Cloud Contracts 中手动编写吗?
- python - 空手道俱乐部:如何从图中提取类
- linux - 如何在 CLion for linux 中构建项目?
- probability - 使用联合概率将贝叶斯网络与网络图匹配
- node.js - 快速路线不可用 - Heroku
- c++ - 如何定义具有两个相对参数的模板函数