Home » Android » android – New navigation component from arch with nested navigation graph

android – New navigation component from arch with nested navigation graph

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?
example

How to&Answers:

The point is to get the right NavController to navigate in the right graph.
Let’s take this scenario as an example:

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

The main graph and the nested graph are in separate xml files: this is required, as far as I understood, because the navigations target different layout areas, so they require two different NavHosts. Each Navhost will need to reference its graph by id, which requires them to be in different resource files.

The point is that to navigate in a specific graph, we must get a reference to the right graph’s owner: to do this, when calling Navigation.findNavController(view), the view argument is crucial.

Docs say that

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

So for example, if inside NavBarFragment we write

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

here view is a parent of the NestedNavHost (that is the nested NavHostFragment), not a descendant, meaning that findNavController will search upstream in the tree and will return the MainNavHost‘s NavController.

If instead we write

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

where nestedNavHostFragment is the id within the fragment tag in the layout, we get a reference to the correct NestedNavHost, because the view we now pass to findNavController belongs to the NestedNavHost‘s subtree.

Similarly, if you need to get a reference to the main NavController from inside a NestedContentFragment, here’s what we can do:

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)
}

Answer:

Actually you could use Global actions to navigate from a nested nav graph destination to a main nav graph destination.

Create a global action from nested nav graph to desired destination in main nav graph (highlighted in the image below)

example:

nav graph

<navigation android:id="@+id/main_nav_graph"
     ... >
     <fragment android:id="@+id/fragStart" .../>
     <fragment .../>
     <fragment .../>

     <navigation  android:id="@+id/nested_nav_graph">
           ...

     <!-- Global Action -->
     <action
         android:id="@+id/action_global_start"
         app:destination="@id/fragStart" />
     </navigation>

</navigation>

To navigate to main graph destination use

findNavController().navigate(R.id.action_global_start)

Answer:

Actually is working,
using

val host: NavHostFragment? = (childFragmentManager.findFragmentById(R.id.main_app_fragment_container)  as NavHostFragment?)

I can navigate from main fragment

Answer:

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)
    }
}

Answer:

Same problem, all examples found on the web iterate over the basic setup.
I have an nav graph in main activity which starts with login fragment, after login goes to main fragment witch has another nav graph with bottom navigation view. The problem is that the main fragment is tied to main activity nav graph. I think only the fragments inside the main fragment can use the main fragment nav graph.