Skip to content

Commit

Permalink
[NavigationBar] Set badges to be null instead of removing them from t…
Browse files Browse the repository at this point in the history
…he sparse array so that removing badges before restoring badge states will not override the current state

PiperOrigin-RevId: 546027617
  • Loading branch information
imhappi authored and paulfthomas committed Jul 7, 2023
1 parent 0fb766e commit 9f2e686
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
11 changes: 4 additions & 7 deletions lib/java/com/google/android/material/badge/BadgeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,7 @@ public static ParcelableSparseArray createParcelableBadgeStates(
for (int i = 0; i < badgeDrawables.size(); i++) {
int key = badgeDrawables.keyAt(i);
BadgeDrawable badgeDrawable = badgeDrawables.valueAt(i);
if (badgeDrawable == null) {
throw new IllegalArgumentException("badgeDrawable cannot be null");
}
badgeStates.put(key, badgeDrawable.getSavedState());
badgeStates.put(key, badgeDrawable != null ? badgeDrawable.getSavedState() : null);
}
return badgeStates;
}
Expand All @@ -291,10 +288,10 @@ public static SparseArray<BadgeDrawable> createBadgeDrawablesFromSavedStates(
for (int i = 0; i < badgeStates.size(); i++) {
int key = badgeStates.keyAt(i);
BadgeState.State savedState = (BadgeState.State) badgeStates.valueAt(i);
if (savedState == null) {
throw new IllegalArgumentException("BadgeDrawable's savedState cannot be null");
BadgeDrawable badgeDrawable = null;
if (savedState != null) {
badgeDrawable = BadgeDrawable.createFromSavedState(context, savedState);
}
BadgeDrawable badgeDrawable = BadgeDrawable.createFromSavedState(context, savedState);
badgeDrawables.put(key, badgeDrawable);
}
return badgeDrawables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,10 @@ void restoreBadgeDrawables(SparseArray<BadgeDrawable> badgeDrawables) {
}
if (buttons != null) {
for (NavigationBarItemView itemView : buttons) {
itemView.setBadge(this.badgeDrawables.get(itemView.getId()));
BadgeDrawable badge = this.badgeDrawables.get(itemView.getId());
if (badge != null) {
itemView.setBadge(badge);
}
}
}
}
Expand Down Expand Up @@ -903,14 +906,11 @@ BadgeDrawable getOrCreateBadge(int menuItemId) {

void removeBadge(int menuItemId) {
validateMenuItemId(menuItemId);
BadgeDrawable badgeDrawable = badgeDrawables.get(menuItemId);
NavigationBarItemView itemView = findItemView(menuItemId);
if (itemView != null) {
itemView.removeBadge();
}
if (badgeDrawable != null) {
badgeDrawables.remove(menuItemId);
}
badgeDrawables.put(menuItemId, null);
}

private void setBadgeIfNeeded(@NonNull NavigationBarItemView child) {
Expand Down

0 comments on commit 9f2e686

Please sign in to comment.