简体   繁体   English

如何修复底部导航栏未正确显示

[英]How to fix bottom navigation bar not showing correctly

I'm having the problem that when I trying to switch between the menu item.当我尝试在菜单项之间切换时遇到问题。 The menu item will not pointing to the correct icon.菜单项不会指向正确的图标。 here is the flow when i found this problem这是我发现这个问题时的流程

  1. starting at the (Home)fragment,then press on the second menu item (Features )从(Home)片段开始,然后按第二个菜单项(Features)
case R.id.nav_home:
    //home fragment transaction
    actionBar.setTitle("Home");
    HomeFragment fragment1 = new HomeFragment();
    FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
    fragmentTransaction1.replace(R.id.content, fragment1, "");
    fragmentTransaction1.commit();
    break;

  1. second menu item (features) will go to features activity第二个菜单项(功能)将 go 到功能活动
case R.id.nav_features:
    //features fragment transaction
    startActivity(new Intent(DashboardActivity.this, FeaturesActivity.class));
    break;
  1. close features activity and back to (Home) fragment关闭功能活动并返回(主页)片段
onBackPressed(); 
  1. bottom navigation still pointing to second menu item (features).底部导航仍然指向第二个菜单项(功能)。

how can I make the system point to the correct menu item?如何使系统指向正确的菜单项? 在此处输入图像描述

You should return a boolean for this method:您应该为此方法返回boolean

 @Override
 public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    //...
 }

do this:做这个:

 case R.id.nav_home:
      //home fragment transaction
      actionBar.setTitle("Home");
      HomeFragment fragment1 = new HomeFragment();
      FragmentTransaction fragmentTransaction1 = 
      getSupportFragmentManager().beginTransaction();
      fragmentTransaction1.replace(R.id.content, fragment1, "");
      fragmentTransaction1.commit();
      return true; // add this line and remove break;

if you don't want to select icon after click, you can return false .如果您不想点击 select 图标后,可以返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM