android - Android Jet Pack Navigation,setupWithNavController() 重新创建片段
问题描述
我使用 jetpack 的 BottomNavBar 的导航视图有问题
所以这就是我的流程是如何工作的。
我有 4 个视图,每个视图都有重定向,比如当我在导航栏的最后一个选择中时,我有一个fragment A
-> fragment B
,当我回到导航栏的第一个选择时,当我回到第 4 个时,它的一个fragment A
再次。我相信这是因为碎片正在使用setupWithNavController()
if 重新创建,所以 jetpack 有解决方法吗?
这是我的代码供参考。
<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/nav_graph"
app:startDestination="@+id/splashFragment">
<fragment
android:id="@+id/selectionFragment"
android:name="whitecloak.com.allibuy.app.selection.SelectionFragment"
android:label="fragment_selection"
tools:layout="@layout/fragment_selection" >
<action
android:id="@+id/toLogin"
app:destination="@id/loginFragment"
app:launchSingleTop="true"
app:popUpTo="@+id/nav_graph"
app:popUpToInclusive="true/>
</fragment>
<fragment
android:id="@+id/splashFragment"
android:name="whitecloak.com.allibuy.app.splash.SplashFragment"
android:label="fragment_splash"
tools:layout="@layout/splash_fragment"
>
<action
android:id="@+id/toMain"
app:destination="@id/mainFragment"
app:launchSingleTop="true"
app:popUpTo="@+id/nav_graph"
app:popUpToInclusive="true"/>
</fragment>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/splashFragment"
android:icon="@drawable/home"
android:title="@string/home"
app:popUpTo="@id/nav_graph" />
<item
android:id="@+id/tabCart"
android:icon="@drawable/cart"
android:title="@string/cart"
app:popUpTo="@id/nav_graph" />
<item
android:id="@+id/tabNotif"
android:icon="@drawable/notification"
android:title="@string/notification"
app:popUpTo="@id/nav_graph"/>
<item
android:id="@+id/selectionFragment"
android:icon="@drawable/user"
android:title="@string/account"
app:popUpTo="@id/nav_graph" />
bottomNav.setupWithNavController(findNavController(R.id.nav_main))
我刚刚包含了第一个和最后一个选项卡的 XML。太感谢了。
编辑
class MainNavigation : DaggerAppCompatActivity() {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var viewModel: MainNavigationViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this, viewModelFactory)[MainNavigationViewModel::class.java]
bottomNav.setupWithNavController(findNavController(R.id.nav_main))
}
}
解决方案
它与您的 BottomNav 的设置无关。它是为 Android 制作的实现中的显式行为。我将引用并解释:
Android 上的行为:应用导航到目的地的顶级屏幕。任何先前的用户交互和临时屏幕状态都会被重置,例如滚动位置、选项卡选择和在线搜索。
来自https://material.io/design/components/bottom-navigation.html#behavior
这意味着当您单击 BottomNav 上的项目时,它应该始终返回到此流堆栈上的第一个片段。
如果我不清楚,这是一个伪表示:
BottomNavItem#1 > Fragment1A > Fragment1B
BottomNavItem#2 > Fragment2A > Fragment2B
如果您点击BottomNavItem#1
,它会加载Fragment1A
。然后想象使用它显示的按钮Fragment1B
。如果您现在单击BottomNavItem#2
,您将看到Fragment2A
。现在,如果您再次单击BottomNavItem#1
,它将显示Fragment1A
(不是Fragment1B
您上次看到的那个),因为它是该堆栈/流的根。
推荐阅读
- c# - 领域驱动设计:订单和订单线类
- javascript - 在 HighCharts 上设置图标(图像)时遇到问题
- python - 如何修复python3,django2中的“意外令牌']'”错误?
- npm - 在 npm 的这些命令中,'@' 是什么意思?
- azure - 如何在 Azure 数据工厂的数据流聚合活动中添加动态 GroupBy 列
- entity-framework - 在 EF Core 中检索数据时如何运行方法
- macos - 关于Tails下载安装的问题
- java - .jar 文件部署 - 无主要方法
- java - 如何找到不再需要的@SuppressFBWarnings 注释?
- android - 在 Android 上使用 Google Smartlock 时的自定义对话框