是否可以将第二个导航图用作嵌套导航图?

Can a second navigation graph be used as a nested navigation graph?

似乎推荐使用新的 android 导航组件。我关注官方tutorial图嵌套

To group destinations into a nested graph, do the following:

In the Navigation Editor, press and hold the Shift key, and click on the destinations you want to include in the nested graph. Right-click to open the context menu, and select Move to Nested Graph > New Graph. The destinations are enclosed in a nested graph. Figure 2 shows a nested graph in the Navigation Editor:

到目前为止一切正常,我只有一个问题。 嵌套图都是一个大 xml 文件的一部分,这是一个问题,因为在使用源代码管理时它会导致合并冲突。

所以我的问题是,如果我们简单地创建两个导航图并将新图包含为目的地,我们是否可以拥有相同的功能? 一切都会按预期工作吗,或者这样做会产生意想不到的后果吗? 例如弹回导航控制器的根目录?我对 Android 不是很熟悉,但我担心会创建一个新的导航控制器,当有两个导航图时我无法轻松导航。

教程中似乎没有提到这一点。

根据 Reference other navigation graphs with <include>:

While this is functionally the same as using a nested graph, include lets you use graphs from other project modules or from library projects

<include> 在运行时扩展为与嵌套图相同的功能 - 在弹出到其他目的地等方面,您可以执行的操作没有区别。

但是请注意,Safe Args does not work across separate navigation graph files (as you could include that graph in multiple other graphs, so there's no guarantee that anything outside of that file actually 'safely' exists), so you would need to confirm that any use of Safe Args is within a single file (using other mechanisms, such as manually navigating or navigating by deep link 导航到您知道存在但无法在编译时验证的图表。