0

I have a BottomNavigationView with menu implement on my MainActivity. I was able to call .showBadge(R.id.firstMenu).setNumber(2); on the BottomNavigationView instance and it shows correctly.

I also have another setting activity that get triggered on the drawer menu. Everything works fine, but when I come back to MainActivity from SettingActivity the badge would never show. I can trace that .showBadge and .setNumber was called.

I'm using com.google.android.material:material:1.1.0-alpha07

Does anyone know what I did wrong?

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);
        }
    }
Khem
  • 1
  • 1
  • 2

1 Answers1

0

put your .showBadge and .setNumber in your onResume() instead of onCreate()

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

}
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
  • That works but. I forgot to tell you that I also implement my a listener method that would trigger on FCM call to update list. (Look at my updated code) This method can trigger down the road with different number but it will never update the number to the correct one. If I never leave the MainActivity in the first place the number will get updated correctly. – Khem Jun 06 '19 at 04:39
  • don't forget that onResume is called also when activity is created first , so you can put the logic your need in your onResume – ismail alaoui Jun 06 '19 at 04:43
  • How do we explain that even if when the listener method is called (and I confirmed this) way after the resume has passed the number won't update? – Khem Jun 06 '19 at 04:47
  • this is really weird ! – ismail alaoui Jun 06 '19 at 15:03