【问题标题】:Navigation for app with multiple modules using single NavHostFragment使用单个 NavHostFragment 为具有多个模块的应用程序导航
【发布时间】:2019-01-22 08:10:04
【问题描述】:

我有一个包含多个功能模块的应用程序,我想在我的主模块中使用单个 NavHostFragment 进行导航。

         app
       |  |  |
feature1  |  feature2
       |  |  |
       common

导航图似乎工作正常(我可以添加功能模块中的所有片段,当我启动应用程序时会显示 startFragment)但有时我必须从功能模块内部开始导航但我没有访问权限到那里的 NavHostFragment。

例如:

findNavController(navHostFragment).navigate(R.id.loginFragment)

【问题讨论】:

    标签: android kotlin android-architecture-components android-architecture-navigation android-module


    【解决方案1】:

    根据findNavController() documentation

    此方法将定位与此 Fragment 关联的 NavController,首先沿给定 Fragment 的父链查找 NavHostFragment。

    这意味着您不需要传入NavHostFragment 本身,但可以传入您的NavHostFragment 创建的任何Fragment,它会正确找到NavController

    因此,在几乎所有情况下,您都应该将this 传递给findNavController()

    // From anywhere in one of the Fragments in your navigation graph
    findNavController(this).navigate(R.id.loginFragment)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-02
      • 2023-01-03
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多