我正在将 Android 的导航架构组件集成到我的应用中。我在将数据从 Activity 传递到 fragment 开头时遇到了一些问题,所以我遵循了这个答案:Navigation Architecture Component- Passing argument data to the startDestination .
逻辑对我来说似乎是合理的,但我正在努力确定如何实际获取 NavHostFragment。 Elliot Shrock 使用了这条线——val navHostFragment = navFragment as NavHostFragment
但我还没有找到有效的 Java 等价物。
我尝试通过使用获取我的 navHost 的 fragment getSupportFragmentManager().findFragmentById(R.id.[mynavhostid])
但此命令返回 null。有任何想法吗?
最佳答案
我解决了这个问题,首先膨胀包含 NavHost 的 View ,然后使用 supportFragmentManager 获取 navHost fragment 并传入我的默认参数。
希望 Google 的 Android 团队在 future 为此提供更清洁的解决方案。
关于android - 如何获取 NavHostFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50884215/