嵌套的 NavHost 是否可以同时作为默认导航主机 (defaultNavHost="true")?

Can nested NavHosts both be the default nav host (defaultNavHost="true")?

我使用两个嵌套的 NavHostFragments 实现了一个导航结构。我希望后退按钮首先弹出子 Fragment 的导航图的导航堆栈,然后一旦回到起点,弹出父 Fragment 的图。当我为父项和子项 FragmentContainerViews.

设置 app:defaultNavHost="true" 时,此行为有效

但是根据 Android 文档:

The app:defaultNavHost="true" attribute ensures that your NavHostFragment intercepts the system Back button. Note that only one NavHost can be the default. If you have multiple hosts in the same layout (two-pane layouts, for example), be sure to specify only one default NavHost.

使用嵌套 NavHost 时此规则是否仍然适用? 当为 multiple/nested NavHost 设置 defaultNavHost="true" 时,我 运行 会遇到什么问题?

是的,'only one defaultNavHost' 的限制仅适用于同一 FragmentManager 中的两个 NavHostFragment(即兄弟姐妹)。嵌套它们完全没问题。