Android Jetpack导航嵌套选项卡向后导航异常行为

时间:2018-07-12 19:23:44

标签: android android-architecture-navigation

所以我正在尝试使用BottomNavigationView的Jetpack导航组件。我创建了两层BottomNavigationView,其结构如下所示:

  • MainActivity(带有nav_host_fragment,navigation_graph,bottom_navigation)
    • FragmentA
    • FragmentB
    • FragmentC(带有nested_nav_host_fragment,nested_navigation_graph,nested_bottom_navigation)
      • FragmentCA
      • FragmentCB
      • FragmentCC

我向前导航没有问题,但是我无法正确向后导航。 例如,当我从A-> B-> C导航,并在C中导航CA-> CB-> CC,然后单击“后退”按钮或调用navControler返回时,它应从CC-> CB-> CA-> B -> A,但直接转到A。

可以找到最小的演示项目here,希望有人可以提供帮助,谢谢。

1 个答案:

答案 0 :(得分:3)

默认情况下,片段弹出添加到后代子片段中的任何内容。

要使系统返回按钮弹出片段C的子片段,您必须通过调用setPrimaryNavigationFragment()具体选择加入该行为。

可以在连接片段后在片段中的任何位置进行此操作。例如,您可以更新FragmentC以在onActivityCreated()中进行操作:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    NavigationUI.setupWithNavController(nested_bottom_navigation,
            activity?.findNavController(R.id.nested_nav_host_fragment)?:return)

    // This routes the system back button to this Fragment
    requireFragmentManager().beginTransaction()
            .setPrimaryNavigationFragment(this)
            .commit()
}

这实际上与app:defaultNavHost="true"上的NavHostFragment属性在幕后使用的技术相同。

相关问题