Skip to content

Commit

Permalink
[Badge] Prevent multiple badges being attached to a menu item at the …
Browse files Browse the repository at this point in the history
…same time

Fixes the logic in NavigationBarMenuView.restoreBadgeDrawables(), which may override the newly created badges with the restored drawables incorrectly. Also adds a safe guard that removes the previously attached badges if any, before attaching a new badge.

PiperOrigin-RevId: 424923078
  • Loading branch information
drchen authored and pekingme committed Feb 1, 2022
1 parent 3cf23e4 commit b3db1cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Expand Up @@ -33,6 +33,7 @@
import androidx.appcompat.view.menu.MenuView;
import androidx.appcompat.widget.TooltipCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -774,6 +775,13 @@ public void setActiveIndicatorResizeable(boolean resizeable) {
}

void setBadge(@NonNull BadgeDrawable badgeDrawable) {
if (this.badgeDrawable == badgeDrawable) {
return;
}
if (hasBadge() && icon != null) {
Log.w("NavigationBar", "Multiple badges shouldn't be attached to one item.");
tryRemoveBadgeFromAnchor(icon);
}
this.badgeDrawable = badgeDrawable;
if (icon != null) {
tryAttachBadgeToAnchor(icon);
Expand Down
Expand Up @@ -797,7 +797,7 @@ void restoreBadgeDrawables(SparseArray<BadgeDrawable> badgeDrawables) {
}
if (buttons != null) {
for (NavigationBarItemView itemView : buttons) {
itemView.setBadge(badgeDrawables.get(itemView.getId()));
itemView.setBadge(this.badgeDrawables.get(itemView.getId()));
}
}
}
Expand Down

0 comments on commit b3db1cc

Please sign in to comment.