带有嵌套导航图的来自弓的新导航组件 [英] New navigation component from arch with nested navigation graph

查看:150
本文介绍了带有嵌套导航图的来自弓的新导航组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个案例,希望通过弓形导航组件来实现.例如,我有2个导航图(主图和嵌套图).我可以从嵌套中调用主图吗?如何调用?

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?

推荐答案

重点是获取正确的NavController以便在正确的图形中导航. 让我们以这种情况为例:

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

主图和嵌套图位于单独的xml文件中:据我所知,这是必需的,因为导航针对不同的布局区域,所以它们需要两个不同的NavHost.每个Navhost都需要按ID引用其图,这要求它们位于不同的资源文件中.

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.

关键是要导航到特定图形,我们必须获得对正确图形所有者的引用:为此,在调用Navigation.findNavController(view)时,view参数至关重要.

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.

医生这么说

NavHostFragments在其视图子树的根处注册其导航控制器,以便任何后代都可以通过Navigation helper类的方法获取控制器实例.

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

例如,如果在NavBarFragment内部,我们写

So for example, if inside NavBarFragment we write

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

此处viewNestedNavHost父级(即嵌套的NavHostFragment),不是后代,这意味着findNavController将在树的上游搜索并返回MainNavHostNavController.

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.

如果我们改为写

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

其中,nestedNavHostFragment是布局中FragmentContainerView的ID,我们获得了对正确的NestedNavHost的引用.请注意使用childFragmentManager,而不是parentFragmentManager.

where nestedNavHostFragment is the id of the FragmentContainerView in the layout, we get a reference to the correct NestedNavHost. Note the use of childFragmentManager, not parentFragmentManager.

如果您仍在使用已弃用的xml <fragment>标记,则可以编写

In case you're still using the deprecated xml <fragment> tag, you can write

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

其中,nestedNavHostFragment<fragment>标记的ID.现在我们获得了对正确的NestedNavHost的引用,因为传递给findNavController的视图属于NestedNavHost的子树.

where nestedNavHostFragment is the id of the <fragment> tag. We get a reference to the correct NestedNavHost now, because the view we pass to findNavController belongs to the NestedNavHost's subtree.

类似地,如果您需要从NestedContentFragment内部获取对主NavController的引用,则可以执行以下操作:

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

这篇关于带有嵌套导航图的来自弓的新导航组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆