Development issue/problem:

I have a business, and I want to implement it with an arc navigation component. For example, I have two cards (head and nested). Can I name the main scheme of the nest and how?

How can I solve this problem?

Solution 1:

It’s about getting the right navigation controller to navigate on the right schedule.
Let’s take this scenario as an example:

Main activity
|- MainNavHost
|- NavBarFragment
|- NestedNavHost
|- NestedContentFragment1
|- NestedContentFragment2
|-
|- BottomNavigationView
|-
|- LoginFragment

The master schedule and the nested schedule are in separate xml files: This is necessary, as I understand it, because the navigations focus on different areas of development and therefore require two different NavHosts. Each navhost must access its own graph based on ID, which requires them to be in different source files.

The fact is that to navigate to a particular graph, you need to get a reference to the owner of the desired graph: when you call Navigation.findNavController(view), the view’s argument is crucial.

Doctors say that

NavHostFragments register their navigation controller at the base of the view subtree, so that each descendant can get an instance of the controller with the methods of the Navigation Aid class.

So, for example, if we write in NavBarFragment

override fun onViewCreated(view: view, savedStudent: package?) {
NavController = Navigation.findNavController(view)
}

Here the view is the parent of NestedNavHost (i.e. the nested NavHostFragment) rather than the descendant, meaning that findNavController will search the tree and NavController will return MainNavHost.

If we write instead

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val nestedNavHostFragment = childFragmentManager.findFragmentById(R.id.nestedNavHostFragment) if ? NavHostFragment
NavController = nestedNavHostFragment ?.NavController
}

where nestedNavHostFragment is the ID of FragmentContainerView in the layout, we get a reference to the correct NestedNavHost. Note the use of the ChildFragmentManager instead of the parentFragmentManager.

If you are still using the xml tag, which is deprecated, you can write

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

where the nested NavHostFragment is the label identifier. We now get a correct NestedNavHost reference, since the image we pass to the NavController for searching belongs to the NestedNavHost subtree.

Similarly, if you want to get a reference to the main marine controller from the NestedContentFragment :

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// we can get the inner NavController through this view,
// because we are in the subtree:
nestedNavController = Navigation.findNavController(view)

// we can find the external NavController that passes the property to activity
// and the view id associated with that NavController,
// for example NavHostFragment id:
mainNavController = Navigation.findNavController(activity !!!, R.id.mainNavHostFragment)
}

Solution 2:

You can actually use global actions to navigate from a nested navigation graph to the main navigation graph.

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

For example:

New navigation component from arch with nested navigation graph –

To navigate to the target of the main schedule, use

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

Solution 3:

I have created an answer using the information provided by Devrocca. It’s the complete answer from the beginning, I didn’t miss anything if anyone needs it.

New navigation component from arch with nested navigation graph –

This is a basic excerpt for navigation. The camera is a direct assignment with no nested graph, the dashboard has its own nested graph but is added to the same rear camera patch. The house has three of them with their own naval captain.

Main Activity|- MainNavHost|- HomeNavHostFragment|- NestedNavHost|- HomeFragment1|- HomeFragment2|- HomeFragment3|- nav_graph_dashboard|- NestNavHost|- CameraFragment

Here are the navigation files

Main navigation Nav_graph.xml

Nested dashboard navigation diagram

And a navigation chart with its own NavHost nav_graph_home

The layouts, I only add the good ones, others are simple layouts with buttons, I add a link to a typical project with other typical navigation elements.

Main activity

Main fragment, this is the first fragment in the image, used as the start of the main navigation.

Layout containing the internal NavHost fragment for home navigation.

MainActivity is designed to control the backstack of the main navigation, the important thing here is that.

The supportFragmentManager reverse stack is not updated when a childFragmentManager is browsing, even if you only have one.

Main activity class : AppCompatActivity() {.

fun override onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Get NavHostFragment
trap NavHostFragment =
supportFragmentManager.findFragmentById(R.id.main_nav_host_fragment)

// ChildFragmentManager NavHostFragment
val navHostChildFragmentManager = navHostFragment?

NavHostChildFragmentManager ?.addOnBackStackChangedListener {

trap backStackEntryCount = navHostChildFragmentManager.backStackEntryCount
trap fragments = navHostChildFragmentManager.fragments
}
}
}

Fragment containing the main navigation node

HomeNavHostClass of fragments : BaseDataBindingFragment() {
decisive fun getLayoutRes() : B = R.layout.fragment_home_navhost

private control device var NavController : NavController? = invalid

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState))

val nestedNavHostFragment =
childFragmentManager.findFragmentById(R.id.nested_nav_host_fragment) if ? NavHostFragment
NavController = nestedNavHostFragment ?.NavController

NavController ?.navigation (R.id.homeFragment1)

listenBackStack()
}

private fun listenBackStack() {

// Get NavHostFragment
trap NavHostFragment =
childFragmentManager.findFragmentById(R.id.nested_nav_host_fragment)

// Current ChildFragmentManager NavHostFragment
val navHostChildFragmentManager = navHostFragment?

NavHostChildFragmentManager ?.addOnBackStackChangedListener {

trap backStackEntryCount = navHostChildFragmentManager!!backStackEntryCount
trap fragments = navHostChildFragmentManager!fragments

Toast.makeText(
requireContext(),
HomeNavHost backStackEntryCount: $backStackEntryCount, fragments: $fragments,
Toast.LENGTH_SHORT
).show()
}

Reminder trap = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {.

val backStackEntryCount = navHostChildFragmentManager !!.backStackEntryCount

Toast.makeText(
requireContext(),
HomeNavHost backStackEntryCount: $backStackEntryCount,
Toast.LENGTH_SHORT
).show()

if (backStackEntryCount == 1) {
[email protected] this.isEnabled = false
requireActivity().onBackPress()
} else {
navController ?.navigateUp()
}
}
} } }

requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback)

}
}

There is one thing I don’t know if it is improved in the graph or in the code with the nested NavHostFragment.

If you set the HomeFragment assignment of Nav_graph_home instead of HomeNavHostFragment1, it acts as a toolbar that ignores nested NavHosts and inserts fragments into the main stack.

Since you are in an internal NavHostFragment, findNavController() returns an internal fragment in any house fragment.

HomeFragmentation Class3 : BaseDataBindingFragment() {
decisive fun getLayoutRes() : B = R.layout.fragment_home3

Article number var = 0

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState))

dataBinding.btnIncrease.setOnClickListener {
dataBinding.tvTitle.text = Account: ${account++}
}

val mainNavController =
Navigation.findNavController(requireActivity(), R.id.main_nav_host_fragment)

dataBinding.btnGoToStart.setOnClickListener {

// using destination belongs to main_nav_host with nested NavHost causes application
to crash // findNavController().navigate(R.id.action_global_start)

mainNavController.navigate(R.id.action_global_start)/**/}}

You can also use global actions, but this is not necessary, as the internal NavHost will take you directly to the main navigation if you are not using OnBackPressed.

Link to the full example and other examples of navigation components if you are interested.

Solution 4:

It actually works.
uses

Master Val: NavHostFragment? = (childFragmentManager.findFragmentById(R.id.main_app_fragment_container) as NavHostFragment?)

I can navigate a simple piece

Solution no. 5:

I have found a temporary solution to the NavController’s internal problem.
You can use a custom NavHostFragment that gives you the NavController you want.
My code:

MyNavHostFragmentation class : NavHostFragment() {

override fun onCreate(storedInstanceState: Bundle?) {
super.onCreate(storedInstanceState)
MainFragment.innerNavController = navController
}
}

Main Fragmentation Class : Fragment() {
companion object{
lateinit var innerNavController : NavController
}

override fun onViewCreated(view : view, savedInstanceState : package ?) {
super.onViewCreated(view, savedInstanceState)
val bottomNavigationView =
view !!.findViewById(R.id.bottom_navigation_view)
bottomNavigationView.setupWithNavController(innerNavController)
}
}

Solution no. 6:

I had the same problem, the error didn’t go away, but the problem of the virtual editor not appearing was solved, so maybe this will help someone else;

If you’re working with Kotlin, check that your dependencies work on the same versions of Kotlin, then rebuild/sync, load a bit, and, at least for me, everything seems to work fine.

Solution no. 7:

Same problem, all examples found in the web iteration via the default settings.
In the main activity, I have a navigation map that starts with the sign-in snippet. After logging in to the main clip, the witch has a different navigation map with a lower navigation display. The problem is that the main fragment is linked to the Nav’s main business plan. I think only the excerpts from the main excerpt can use the excerpt from the main navigation graph.

Good luck!

android multiple navigation graphsnavigation graph android examplenavhostfragment inside fragmentandroid navigation graph global actionandroid-navigation nested graph argumentsandroid navigation component reuse fragmentandroid navigation component deep linknavdeeplinkbuilder not workingnavigation-fragment android kotlinandroid jetpack navigation in kotlinandroid navigation component replace fragmentnavigation component java tutorialnavigation component example javanavigation-component-example githubnavigation 2.1 0 tutorialnavigation graph android example javaandroid jetpack navigation example in javaandroid navigation component login flowandroid navigation between activitiesbookmanstreasurenavigation-fragment android tutorialandroid single activity bottom navigationandroid navigation component backstackandroid navigation component global actionsmultiple nav graphsandroid navigation component multiple activitiesnavigation component with toolbar

You May Also Like

[All Fixes] Steam Not Enough Disk Space

This message contains partner links for which we can receive a commission…

What is Apple’s iOS? Introduction to apple’s iOS it’s features and application 2021

First: What is an operating system? A mobile operating system (OS) is…

How to Power Off & Restart Samsung Galaxy S21, S21+ and S21 Ultra

Samsung has finally released the flagship of its 2021 smartphone series –…

Zoom Camera Not Working on Lenovo laptop? (7 Quick solutions to fix it)

Since the days of Covid 19, Zoom (one of the most popular…