onNavigationItemSelected is not called when the selected menu item is same

onNavigationItemSelected is not called when the selected menu item is same

我有一个简单的 BottomNavigationView,在 activity 中有两个菜单项(Home Fragment、Settings Fragment)。

我已经实现了 onNavigationItemSelectedListeneronNavigationItemSelected

还有bottomNavigationView.setOnNavigationItemSelectedListener(this);

应用页面登陆主页片段。

onNavigationItemSelected 当我在菜单项之间切换时被调用,但是当我第一次启动应用程序并点击相同的菜单项,即 Home Fragment 时,onNavigationItemSelected 没有被调用。

当用户已经在主页但未触发 onNavigationItemSelected 事件时,每当用户单击主页时,我都需要显示祝酒辞。

首先我们在 MainActivity 中执行此操作

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int v = item.getItemId();
        if(v==R.id.home)
        {
            getSupportActionBar().setTitle("Home");
            Fragment fragment = new HomeFragment();
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
        }
        else if (v==R.id.dash_board)
        {
            getSupportActionBar().setTitle("Dashboard");
            Fragment fragment = new DashboardFragment();
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().replace(R.id.frame_layout, fragment).commit();
        }
   return true;
    }
};

This is a example here 如果您想详细了解,那么click here

正如 Mike M 提到的,

setOnNavigationItemReselectedListener 成功了。