public interface

MenuProvider

 androidx.core.view.MenuProvider

Gradle dependencies

compile group: 'androidx.core', name: 'core', version: '1.9.0-alpha04'

  • groupId: androidx.core
  • artifactId: core
  • version: 1.9.0-alpha04

Artifact androidx.core:core:1.9.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.core:core com.android.support:support-compat

Overview

Interface for indicating that a component will be supplying MenuItems to the component owning the app bar.

Summary

Methods
public voidonCreateMenu(Menu menu, MenuInflater menuInflater)

Called by the MenuHost to allow the MenuProvider to inflate MenuItems into the menu.

public voidonMenuClosed(Menu menu)

Called by the MenuHost when the is closed.

public booleanonMenuItemSelected(MenuItem menuItem)

Called by the MenuHost when a MenuItem is selected from the menu.

public voidonPrepareMenu(Menu menu)

Called by the MenuHost right before the is shown.

Methods

public void onPrepareMenu(Menu menu)

Called by the MenuHost right before the is shown. This should be called when the menu has been dynamically updated.

Parameters:

menu: the menu that is to be prepared

See also: MenuProvider.onCreateMenu(Menu, MenuInflater)

public void onCreateMenu(Menu menu, MenuInflater menuInflater)

Called by the MenuHost to allow the MenuProvider to inflate MenuItems into the menu.

Parameters:

menu: the menu to inflate the new menu items into
menuInflater: the inflater to be used to inflate the updated menu

public boolean onMenuItemSelected(MenuItem menuItem)

Called by the MenuHost when a MenuItem is selected from the menu.

Parameters:

menuItem: the menu item that was selected

Returns:

true if the given menu item is handled by this menu provider, false otherwise

public void onMenuClosed(Menu menu)

Called by the MenuHost when the is closed.

Parameters:

menu: the menu that was closed

Source

/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.core.view;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;

/**
 * Interface for indicating that a component will be supplying
 * {@link MenuItem}s to the component owning the app bar.
 */
public interface MenuProvider {

    /**
     * Called by the {@link MenuHost} right before the {@link Menu} is shown.
     * This should be called when the menu has been dynamically updated.
     *
     * @param menu the menu that is to be prepared
     * @see #onCreateMenu(Menu, MenuInflater)
     */
    default void onPrepareMenu(@NonNull Menu menu) {}

    /**
     * Called by the {@link MenuHost} to allow the {@link MenuProvider}
     * to inflate {@link MenuItem}s into the menu.
     *
     * @param menu         the menu to inflate the new menu items into
     * @param menuInflater the inflater to be used to inflate the updated menu
     */
    void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater);

    /**
     * Called by the {@link MenuHost} when a {@link MenuItem} is selected from the menu.
     *
     * @param menuItem the menu item that was selected
     * @return {@code true} if the given menu item is handled by this menu provider,
     *         {@code false} otherwise
     */
    boolean onMenuItemSelected(@NonNull MenuItem menuItem);

    /**
     * Called by the {@link MenuHost} when the {@link Menu} is closed.
     *
     * @param menu the menu that was closed
     */
    default void onMenuClosed(@NonNull Menu menu) {}
}