我在尝试在菜单项之间切换时遇到问题。菜单项不会指向正确的图标。 这是我发现此问题时的流程
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;
case R.id.nav_features:
//features fragment transaction
startActivity(new Intent(DashboardActivity.this, FeaturesActivity.class));
break;
onBackPressed();
答案 0 :(得分:2)
您应为此方法返回boolean
:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//...
}
执行此操作:
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;
如果您不想在单击后选择图标,则可以返回false
。