2

I have a Bottom Navigation view with a badge drawable that shows new chats.

This badges are updated upon a listener to Firebase database, where I store the notification counter. When this value change, the badge is updated. Moreover if the counter is equal to zero, the badge is set not visible.

Everything works fine except if I change some configuration using the device Settings (such as language or removing permissions). In fact, if I do that and go back to the app, the activity is re-created (sometimes without destroying it) and badge reloaded. But the setVisibility seems not working. Even if the counter is zero the badge is visible. Plus is not update anymore when the listener is triggered. The code works, I checked with some logs if the listener is triggered and if the lines which include setVisibility are run. It just seems to have random behaviour.

If the activity is destroyed and recreated again, it works.

Any help will be appreciated!

this is how I initialize the badge

    bottomNav = findViewById(R.id.bottom_navigation);
    badge_chat = bottomNav.getOrCreateBadge(R.id.nav_chat);
    badge_chat.setVisible(false);

this is the listener code

         public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            int badge_counter = dataSnapshot.getValue(int.class);
            boolean visibility;
            if (badge_counter == 0) {
                visibility = false;
            } else {
                visibility = true;
            }
            badge_chat.setNumber(badge_counter);
            badge_chat.setVisible(visibility);

        }
E. Tocchi
  • 47
  • 1
  • 6

1 Answers1

3

One way I've managed to resolve this is to create/remove badge whenever it needs to be visible/hidden. In your case, something like this should work:

 if (badge_counter == 0) {
            getBadge(R.id.nav_chat)?.isVisible = false
            removeBadge(R.id.nav_chat)
        } else {
            getOrCreateBadge(R.id.nav_chat).apply{
                 isVisible = true
                 number = badge_counter
                 }
        }

note: the answer is in Kotlin.

ViksaaSkool
  • 738
  • 4
  • 18