具有多个顶级目的地的导航图 [英] Navigation graph with multiple top level destinations

查看:276
本文介绍了具有多个顶级目的地的导航图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在业余时间实现了一个Android应用程序(在Kotlin中,但这与问题无关),我尝试使用android jetpack和新库.我有一个带有导航抽屉的活动.我尝试遵循示例向日葵应用.它在主要活动中使用以下组合来启用导航抽屉背后的逻辑:

appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
setSupportActionBar(findViewById(R.id.toolbar))
setupActionBarWithNavController(navController, appBarConfiguration)

此代码注释:在导航抽屉中单击时,它将自动导航到正确的片段,然后关闭抽屉并保持选中状态,等等.所有这些样板代码.那很整洁,也行得通.据我了解,导航抽屉菜单项的ID必须与导航图中的片段的ID匹配,并通过这种方式将它们连接起来.

我遇到的问题:当我使用导航抽屉转到导航图的起始片段以外的任何片段时,它将显示一个后退按钮,而不是汉堡包项目.那不是我所期望的,我希望它仍然是汉堡包,因为导航抽屉用于在相同级别的视图之间导航,而不是彼此嵌套,对吗?如果我通过单击片段中的元素(例如,列表->详细信息)导航到任何片段的子片段,则希望有一个后退按钮,但是如果使用导航抽屉进行导航,则不会.

现在,我可以将问题追溯到AppBarConfiguration生成器,该生成器读取带有navgraph The NavGraph whose start destination should be considered the only top level destination.的构造函数,通过覆盖AppBarConfiguration以返回不同的顶级目的地而不是仅返回起始目的地,可以很容易地解决此问题.导航图.

但是我的问题是,为什么此行为默认设置?是虫子吗?如果我不遵守此规定,是否会违反Google的某些设计准则?导航抽屉中的每个元素是否都应该与我期望的处于同一水平?我有想要解决的其他解决方案吗?

解决方案

您不必重写AppBarConfiguration.从版本alpha7开始,AppBarConfiguration具有用于所有顶级目标的带有一组ID的构造函数.

Set<Integer> topLevelDestinations = new HashSet<>();
topLevelDestinations.add(R.id.fragment1);
topLevelDestinations.add(R.id.fragment2);
appBarConfiguration = new AppBarConfiguration.Builder(topLevelDestinations)
                                             .setDrawerLayout(drawerLayout)
                                             .build();
NavigationUI.setupActionBarWithNavController(this, 
                                             this.navController,
                                             this.appBarConfiguration);

这不是默认设置,因为导航图只有一个起始片段,应该始终是应用程序的单个入口点.

使用AppBarConfiguration编辑默认行为不会使其像以前一样工作,每个顶级片段都放在后堆栈上,因此后退按钮将转到所有顶级片段.目前尚不清楚如何将顶级片段作为后堆栈的第一个元素.

I am implementing an android app (in Kotlin, but that is not relevant to the Problem) in my free time and I try to use android jetpack and new libraries. I have a single Activity with a navigation drawer. I try to follow the sample sunflower app. It uses the following combination in the main activity to enable the logic behind the navigation drawer:

appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
setSupportActionBar(findViewById(R.id.toolbar))
setupActionBarWithNavController(navController, appBarConfiguration)

Note on this code: This automatically will navigate to the correct fragments when clicked in the navigation drawer and close the drawer and keep them selected etc. All that boilerplate code. That is pretty neat and also works. As far as I understand this, the IDs of the navigation drawer menu items have to match the ids of the fragments in the navigation graph and this way they are connected.

The problem I have: When I use the navigation drawer to go to any fragment other than the starting fragment of the navigation graph, it will display a back button instead of the hamburger item. That is not what I expect, I would expect it still to be the hamburger item since the navigation drawer is for navigating between views on an equal level and not nested in each other, right? I expect a back button if I navigate to a subfragment of any fragment by clicking on elements in that fragment (for example list -> detail) but not if I navigate using the navigation drawer.

Now I traced that problem back to the AppBarConfiguration builder which reads on the constructor taking a navgraph The NavGraph whose start destination should be considered the only top level destination. I can fairly easily fix that by overriding AppBarConfiguration to return different top level destinations than just the starting destination of the navigation graph.

However my question is, why is there this behaviour default? Is it a bug? If I override this will I violate some design guidelines by Google? Should not every element in the navigation drawer be on the same level how I expect it? Is there a different solution inteded for I want to do?

解决方案

You don't have to override AppBarConfiguration. Since version alpha7 AppBarConfiguration has a constructor with a set of ids for all top level destinations.

Set<Integer> topLevelDestinations = new HashSet<>();
topLevelDestinations.add(R.id.fragment1);
topLevelDestinations.add(R.id.fragment2);
appBarConfiguration = new AppBarConfiguration.Builder(topLevelDestinations)
                                             .setDrawerLayout(drawerLayout)
                                             .build();
NavigationUI.setupActionBarWithNavController(this, 
                                             this.navController,
                                             this.appBarConfiguration);

This is not default as the navigation graph has only a single start fragment which should always be the single entry point of the application.

Editing the default behavior with AppBarConfiguration does not make it behave as before, every top level fragment is placed on the back stack so back button will go to all top level fragments. It is unclear how I can make top level fragments as the first element of the back stack.

这篇关于具有多个顶级目的地的导航图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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