从其他活动返回时,Android底部导航菜单上的徽章消失

时间:2019-06-06 03:16:43

标签: android material-design bottomnavigationview badge

我的BottomNavigationView上有一个带有菜单工具的MainActivity。我能够在.showBadge(R.id.firstMenu).setNumber(2);实例上调用BottomNavigationView,并且显示正确。

我还有另一个设置活动,在抽屉菜单上被触发。一切正常,但是当我从MainActivity回到SettingActivity时,徽章将永远不会显示。我可以追踪到.showBadge.setNumber被呼叫了。

我正在使用com.google.android.material:material:1.1.0-alpha07

有人知道我做错了吗?

MainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Init Drawer Menu
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        // Init Fragments
        initFragments();

        bottomNavigationView =(BottomNavigationView) findViewById(R.id.main_bottom_navbar);

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.firstMenu:
                    fragmentManager.beginTransaction().hide(activeFragment).show(fragment1).commit();
                    activeFragment = fragment1;
                    return true;
                case R.id.secondMenu:
                    fragmentManager.beginTransaction().hide(activeFragment).show(fragment2).commit();
                    activeFragment = fragment2;
                    return true;
            }
            return false;
            }
        });
        bottomNavigationView.showBadge(R.id.firstMenu).setNumber(2);
}

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_logout) {
            logout();
        } else if (id == R.id.nav_setting) {
            Intent settingIntent = new Intent(getApplicationContext(), SettingActivity.class);
            startActivity(settingIntent);

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

@Override
    public void OnListUpdate(int newCount) {
        if (newCount > 0) {
            bottomNavigationView.showBadge(R.id.firstMenu).setNumber(newCount);
        } else {
            bottomNavigationView.removeBadge(R.id.firstMenu);
        }
    }

1 个答案:

答案 0 :(得分:0)

将您的.showBadge.setNumber放在onResume()中,而不是onCreate()

@Override
public void onResume(){
    super.onResume();
    bottomNavigationView.showBadge(R.id.firstMenu).setNumber(2);

}