我正在尝试从 导航对话 至 fragment 带有导航组件。我面临导航后对话框自动弹出的问题,因为它实现了 androidx.navigation.FloatingWindow 接口(interface)(文档链接 - https://developer.android.com/guide/navigation/navigation-navigate)。
所以,我想保持底层对话框打开。实现这一目标的最佳选择是什么?
我的导航图:
<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/return_order_dialog_nested_nav_graph"
app:startDestination="@id/returnOrderBottomDialog">
<dialog
android:id="@+id/returnOrderBottomDialog"
android:name="ru.dpd.features.returnOrder.ReturnOrderBottomDialog"
android:label="ReturnOrderBottomDialog"
tools:layout="@layout/dialog_bottom_return_order">
<argument
android:name="orderId"
app:argType="string" />
<action
android:id="@+id/action_returnOrderBottomDialog_to_returnOrderDepartmentMapFragment"
app:destination="@id/returnOrderDepartmentMapFragment" />
</dialog>
<fragment
android:id="@+id/returnOrderDepartmentMapFragment"
android:name="ru.dpd.features.returnOrder.tabs.map.ReturnOrderDepartmentMapFragment"
android:label="ReturnOrderDepartmentMapFragment"
tools:layout="@layout/fragment_departments_map">
<argument
android:name="orderId"
app:argType="string" />
<argument android:name="cityId" />
</fragment>
</navigation>
最佳答案
没有官方解决方案,这是一种解决方法。我尝试使用容器 fragment 作为后台堆栈中的条目/节点。
实际上,当它出现在用户面前时容器启动对话框(在导航或弹出窗口中)
容器 fragment 没有 View 。它在 onCreateView
中启动对话框.所有对话参数 (orderId,..)
必须传递给容器 fragment ,然后手动传递给对话框。
class MyDialogContainerFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// launch the dialog
findNavController(this).navigate(....)
return null
}
}
关于android - 如何使用导航组件保持对话框打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67498750/