New navigation component from arch with nested navigation graph

后端 未结 7 1073
栀梦
栀梦 2020-12-07 11:26

I have one case and wish to implement it by arch navigation component. For example I have 2 Nav Graphs (main and nested). Can I call main graph from nested and how?

相关标签:
7条回答
  • 2020-12-07 12:06

    I found a temporary solution to the problem of inner NavController being covered. You can use custom NavHostFragment which provides you with desired navController. My code:

    <androidx.fragment.app.FragmentContainerView
            ...
            android:name="MyNavHostFragment"
            app:defaultNavHost="false"
            app:navGraph="@navigation/inner_nav">
            ...
        </androidx.fragment.app.FragmentContainerView>
    

    ...

    class MyNavHostFragment: NavHostFragment() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            MainFragment.innerNavController = navController
        }
    }
    

    ...

    class MainFragment : Fragment() {
        companion object{
            lateinit var innerNavController: NavController
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            val bottomNavigationView = 
                 view!!.findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
            bottomNavigationView.setupWithNavController(innerNavController)
        }
    }
    
    0 讨论(0)
提交回复
热议问题