New navigation component from arch with nested navigation graph

后端 未结 7 1192
栀梦
栀梦 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:

    
            ...
        
    

    ...

    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(R.id.bottom_navigation_view)
            bottomNavigationView.setupWithNavController(innerNavController)
        }
    }
    

提交回复
热议问题