在片段之间导航时,当前片段显示在底部(图标突出显示)。但是,当您按下“后退”按钮并在堆栈上移动时,底部导航不起作用,并且冻结了最后一个手动按下的元素。如何解决?
public class MainActivity extends AppCompatActivity {
Fragment selectedFragment = new AFragment();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new AFragment();
break;
case R.id.nav_search:
selectedFragment = new BFragment();
break;
case R.id.nav_add:
startActivity(new Intent(getBaseContext(), CreateMeetActivity.class));
setBorderActive(v_3);
break;
case R.id.nav_notifications:
selectedFragment = new CFragment();
break;
case R.id.nav_profile:
selectedFragment = new DFragment();
break;
default:
break;
}
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}