1

I implemented a navigation structure using two nested NavHostFragmentss. I want the back button to first pop the navigation stack of the child Fragment's navigation graph, and then once back to the start, pop the parent Fragment's graph. This behaviour works when I set app:defaultNavHost="true" for both the parent and child FragmentContainerViews.

But according to the Android documentation:

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.

Is this rule still applicable when using nested NavHosts? What problems could I run into when setting defaultNavHost="true" for multiple/nested NavHosts?

4

1 回答 1

2

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

于 2020-03-05T17:18:07.900 回答