gpt4 book ai didi

android - 检查 NavDestination 是否代表 DialogFragment

转载 作者:行者123 更新时间:2023-12-05 00:13:19 24 4
gpt4 key购买 nike

我有一个围绕 CoordinatorLayout 中的 fragment 容器的 Activity 布局。切换目标时,我想隐藏 FAB 并关闭 SnackBar(如果当前显示其中一个),并让目标 Fragment 在其 onViewCreated() 中重新显示 FAB(如果需要)。这与 Material Design 指南一致,即当您切换目的地时,FAB 应该明显消失并重新出现。

所以我这样做了:

navController.addOnDestinationChangedListener { _, navDestination, _ ->
binding.floatingActionButton.apply {
hide()
setOnClickListener(null)
}
currentSnackbar?.dismiss()
//...
}

但是,当 DialogFragment 是导航目标时,前一个 Fragment 在返回时不会再次调用 onViewCreated(),因为它从未离开过屏幕。

NavDestination 似乎没有任何方法来检查它是否表示 DialogFragment,即使 XML 区分 fragmentdialog

我意识到我可以将显示代码的 FAB 移动到 fragment 中的 onResume,但是在仅打开警报对话框时首先隐藏它看起来很愚蠢。我意识到我也可以创建一个将它隐藏在 onStop() 中的父 Fragment 类,但我尽量避免在可能的情况下创建类层次结构层以使代码不那么脆弱(继承组合),而且我也希望避免在整个应用程序中重复代码。

最佳答案

对话框目标实现 FloatingWindow interface :

A marker interface for NavDestination subclasses that float above the view of other destinations (i.e. DialogFragmentNavigator.Destination).

NavController.OnDestinationChangedListener instances can also customize their behavior based on whether the destination is a FloatingWindow.

因此,您可以使用 if (!(navDestination is FloatingWindow)) 忽略导航到对话框。

关于android - 检查 NavDestination 是否代表 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65204366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com