我有底部导航,带有导航控制器的导航抽屉设置。我目前使用的是最新的v2.4.0-alpha05,它分别管理每个底部导航选项卡的后台。问题是,我有一个自定义的导航抽屉(带有一堆文本视图的linearlayout(,它属于活动本身,而不是片段。这个抽屉里有一个配置文件选项,当我点击它时,我想导航到帐户片段。好吧,我可以用navController.navigate(R.id.action_global_accountFragment)
做到这一点,但这个片段现在将添加到主页片段的背面。每当我切换回我的主页片段(通过底部导航(时,我只想要主页片段的backpack,而不是账户片段,也就是说,每当我点击个人资料视图时,我希望它从堆栈中删除,或者以任何方式切换到账户片段的backack。有什么办法我可以做这两件事或任何其他解决方案吗?我附加了我的主活动oncreate和navcontroller的onDestinationChanged方法。感谢您的帮助。
NavHostFragment navHostFragment =
(NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.nav_host_fragment_activity_main);
navController = navHostFragment.getNavController();
setSupportActionBar(toolbar);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.homeFragment, R.id.listingsFragment, R.id.featuredFragment, R.id.categoriesFragment
, R.id.accountFragment)
.setOpenableLayout(drawerLayout)
.build();
Objects.requireNonNull(getSupportActionBar()).setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
NavigationUI.setupWithNavController(toolbar,navController,appBarConfiguration);
NavigationUI.setupWithNavController(navViewBottom,navController);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.homeFragment){
controller.popBackStack(R.id.homeFragment,false, true); //popping these so that if account fragment is in stack, it will get popped as well, but that also pops all other fragments
Objects.requireNonNull(getSupportActionBar()).show();
Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
}
if (destination.getId() == R.id.categoriesFragment){
controller.popBackStack(R.id.categoriesFragment,false);
Objects.requireNonNull(getSupportActionBar()).show();
Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
}
if (destination.getId() == R.id.listingsFragment){
controller.popBackStack(R.id.listingsFragment,false);
Objects.requireNonNull(getSupportActionBar()).show();
Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
}
if (destination.getId() == R.id.featuredFragment) {
controller.popBackStack(R.id.featuredFragment,false);
Objects.requireNonNull(getSupportActionBar()).show();
Objects.requireNonNull(getSupportActionBar()).setHomeAsUpIndicator(R.drawable.ic_nav_menu_icon);
}
if (destination.getId() == R.id.accountFragment){
controller.popBackStack(R.id.accountFragment,false);
Objects.requireNonNull(getSupportActionBar()).hide();
}
}
});
这是我想要导航到账户片段的布局的onclick
@Override
public void onClick(View view) {
if (view.getId() == R.id.relativeLayout_profile_navDrawer) {
drawerLayout.closeDrawer(GravityCompat.START);
navController.navigate(R.id.action_global_account);
}
}
我刚刚设置了bottomNavView.setSelectedItemId(R.id.yourwantedfargment)
,它为我完成了任务!