Skip to content

Commit

Permalink
[MaterialToolbar] Override inflateMenu() to stop dispatching item cha…
Browse files Browse the repository at this point in the history
…nges during inflation for a performance improvement

PiperOrigin-RevId: 503995162
  • Loading branch information
dsn5ft authored and afohrman committed Jan 24, 2023
1 parent 6f0a953 commit 86600c2
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/java/com/google/android/material/appbar/MaterialToolbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION_CODES;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.Menu;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.ImageView;
Expand Down Expand Up @@ -127,6 +129,19 @@ public MaterialToolbar(@NonNull Context context, @Nullable AttributeSet attrs, i
initBackground(context);
}

@Override
public void inflateMenu(int i) {
// Pause dispatching item changes during inflation to improve performance.
Menu menu = getMenu();
if (menu instanceof MenuBuilder) {
((MenuBuilder) menu).stopDispatchingItemsChanged();
}
super.inflateMenu(i);
if (menu instanceof MenuBuilder) {
((MenuBuilder) menu).startDispatchingItemsChanged();
}
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Expand Down

0 comments on commit 86600c2

Please sign in to comment.