1 /* 2 * Copyright 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.core.view; 18 19 import android.view.Menu; 20 import android.view.MenuInflater; 21 import android.view.MenuItem; 22 23 import org.jspecify.annotations.NonNull; 24 25 /** 26 * Interface for indicating that a component will be supplying 27 * {@link MenuItem}s to the component owning the app bar. 28 */ 29 public interface MenuProvider { 30 31 /** 32 * Called by the {@link MenuHost} right before the {@link Menu} is shown. 33 * This should be called when the menu has been dynamically updated. 34 * 35 * @param menu the menu that is to be prepared 36 * @see #onCreateMenu(Menu, MenuInflater) 37 */ onPrepareMenu(@onNull Menu menu)38 default void onPrepareMenu(@NonNull Menu menu) {} 39 40 /** 41 * Called by the {@link MenuHost} to allow the {@link MenuProvider} 42 * to inflate {@link MenuItem}s into the menu. 43 * 44 * @param menu the menu to inflate the new menu items into 45 * @param menuInflater the inflater to be used to inflate the updated menu 46 */ onCreateMenu(@onNull Menu menu, @NonNull MenuInflater menuInflater)47 void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater); 48 49 /** 50 * Called by the {@link MenuHost} when a {@link MenuItem} is selected from the menu. 51 * 52 * @param menuItem the menu item that was selected 53 * @return {@code true} if the given menu item is handled by this menu provider, 54 * {@code false} otherwise 55 */ onMenuItemSelected(@onNull MenuItem menuItem)56 boolean onMenuItemSelected(@NonNull MenuItem menuItem); 57 58 /** 59 * Called by the {@link MenuHost} when the {@link Menu} is closed. 60 * 61 * @param menu the menu that was closed 62 */ onMenuClosed(@onNull Menu menu)63 default void onMenuClosed(@NonNull Menu menu) {} 64 } 65