Search code examples
androidandroid-fragmentsbottomnavigationviewandroid-jetpackandroid-jetpack-navigation

How to setup Multiple nested FragmentContainerViews with respective navigation graphs?


I was looking into converting my app to single activity as per google's guidelines. (Using Kotlin)

Let's say i have FragmentContainerView1 in my activity with no toolbar or anything else.
This view is used to navigate between login screen,sign up and display the main fragment.

Inside the main fragment, I have another FragmentContainerView2 which should display according to a BottomNavigation view inside main fragment.

But then i have some elements inside the fragments in FragmentContainerView2, that need to perform actions in FragmentContainerView1.

How do i achieve this?

Picture :
Here mainFragment contains the BottomNavigation,of which mainMenuFragment is a destination,which has to perform actions in FragmentContainerView1. How can I get a reference of the inner nav controller of the BottomNavigation?

Edit 1: navController=Navigation.findNavController(view.findViewById(R.id.fragmentContainer)) doesn't work, still returns same outer nav controller. Main Nav Graph


Solution

  • Not a Complete Solution, But the answer I found was to keep one of the containers as a <fragment/> and the other as FragmentContainerView.

    And to get the inner nav controller use:

    innerNavController= (activity as AppCompatActivity).findNavController(R.id.innerFragmentContainer)
    

    Only works with that specific command and one of the containers being <fragment/>.

    Also after getting the innerNavController, Any type of call with findNavController() gives the inner nav controller.
    To get the outerNavController use:

    outerNavController = (activity as AppCompatActivity).findNavController(R.id.outerFragmentContainer)