android - 带有嵌套导航图的 Arch 新导航组件

标签 android android-architecture-components android-navigation android-jetpack

我有一个案例,希望通过拱形导航组件来实现。例如,我有 2 个导航图(主图和嵌套图)。我可以从嵌套调用主图吗?如何调用? enter image description here

最佳答案

关键是要正确NavController在正确的图表中导航。 我们以这个场景为例:

MainActivity
|- MainNavHost
   |- NavBarFragment
   |  |- NestedNavHost
   |  |  |-NestedContentFragment1
   |  |  |-NestedContentFragment2
   |  |
   |  |- BottomNavigationView
   |
   |- LoginFragment

主图和嵌套图在单独的xml文件中:据我了解,这是必需的,因为导航针对不同的布局区域,因此它们需要两个不同的NavHost s。每个Navhost需要通过 id 引用它的图,这要求它们位于不同的资源文件中。

关键是要在特定的图表中导航,我们必须获得对正确图表所有者的引用:为此,在调用 Navigation.findNavController(view) 时, view论据至关重要。

文档这么说

NavHostFragments register their navigation controller at the root of their view subtree such that any descendant can obtain the controller instance through the Navigation helper class's methods

例如,如果在 NavBarFragment我们写

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    navController = Navigation.findNavController(view)
}

这里 viewNestedNavHost父级 (即嵌套的 NavHostFragment ),不是后代,这意味着 findNavController将在树中搜索上游并返回 MainNavHostNavController .

如果我们改为写

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val nestedNavHostFragment = childFragmentManager.findFragmentById(R.id.nestedNavHostFragment) as? NavHostFragment
    navController = nestedNavHostFragment?.navController
}

在哪里 nestedNavHostFragmentFragmentContainerView 的 id在布局中,我们得到了正确的 NestedNavHost 的引用。 .注意 childFragmentManager 的使用,而不是 parentFragmentManager .

如果您仍在使用已弃用的 xml <fragment>标签,你可以写

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val fragmentContainer = view.findViewById<View>(R.id.nestedNavHostFragment)
    navController = Navigation.findNavController(fragmentContainer)
}

在哪里 nestedNavHostFragment<fragment> 的 id标签。我们得到了正确的 NestedNavHost 的引用。现在,因为我们传递给 findNavController 的 View 属于 NestedNavHost的子树。

同样,如果您需要获取对主要 NavController 的引用从 NestedContentFragment 内部,这是我们可以做的:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    // we can get the innermost NavController using this view,
    // because we are inside its subtree:
    nestedNavController = Navigation.findNavController(view)

    // we can find the outer NavController passing the owning Activity
    // and the id of a view associated to that NavController,
    // for example the NavHostFragment id:
    mainNavController = Navigation.findNavController(activity!!, R.id.mainNavHostFragment)
}

关于android - 带有嵌套导航图的 Arch 新导航组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50730494/

相关文章:

android - 如果 Recycler View Item Click 置于 Swipe Refresh Layout 下则无法正常工作 - Android

java - ViewPager 卡住了

java - 通过单击 imageView 打开 Activity

java - 如何将 PreferenceFragment 与 ViewPager 一起使用?

android - 为什么在重新创建 Activity 时我的 savedInstanceState 为 Null?

Android Studio 3.2 金丝雀版本不呈现导航标签

android - 导航拱组件 : Passing placeholder param for deeplink

屏幕旋转后 Android Arch 组件 ViewModel 和 LiveData 触发

android - 将 LiveData 转换为 MutableLiveData

java - 此 NavController 不知道操作