如何用另一个片段替换nav_host_fragment中的片段

问题描述

NavController navController = Navigation.findNavController(this,R.id.nav_host_fragment);
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home,R.id.nav_events,R.id.nav_slideshow,R.id.search,R.id.profile).setDrawerLayout(drawer).build();
NavigationUI.setupActionBarWithNavController(this,navController,mAppBarConfiguration);

NavigationUI.setupWithNavController(navigationView,navController);

我想在navhost片段中添加一个片段,例如通知片段,但它不在导航菜单中,而是在工具栏顶部。

所以我想,当我点击通知图标时,nav主机片段中的notification_fragment应该被夸大。

解决方法

navController.navigate(R.id.mainFragment,bundle);

您可以使用上面的代码来更改 onclick 侦听器中的片段。 R.id.mainFragment 将是您要显示的片段的 ID。您必须将此片段添加到导航图中。 'bundle' 是可选的,如果您想将任何数据传递给传入的片段。