public interface

MenuHost

 androidx.core.view.MenuHost

Subclasses:

CarAppPermissionActivity, CarAppActivity, SlicePermissionActivity, AppCompatActivity, Toolbar, DeviceCredentialHandlerActivity, ComponentActivity, FragmentActivity, FragmentScenario.EmptyFragmentActivity

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

A class that allows you to host and keep track of MenuProviders that will supply android.view.MenuItems to the app bar.

Summary

Methods
public voidaddMenuProvider(MenuProvider provider)

Adds the given MenuProvider to this MenuHost.

public voidaddMenuProvider(MenuProvider provider, LifecycleOwner owner)

Adds the given MenuProvider to this MenuHost.

public voidaddMenuProvider(MenuProvider provider, LifecycleOwner owner, Lifecycle.State state)

Adds the given MenuProvider to this MenuHost once the given LifecycleOwner reaches the given .

public voidinvalidateMenu()

Invalidates the to ensure that what is displayed matches the current internal state of the menu.

public voidremoveMenuProvider(MenuProvider provider)

Removes the given MenuProvider from this MenuHost.

Methods

public void addMenuProvider(MenuProvider provider)

Adds the given MenuProvider to this MenuHost. If using this method, you must manually remove the provider when necessary.

Parameters:

provider: the MenuProvider to be added

See also: MenuHost.removeMenuProvider(MenuProvider)

public void addMenuProvider(MenuProvider provider, LifecycleOwner owner)

Adds the given MenuProvider to this MenuHost. This MenuProvider will be removed once the given LifecycleOwner receives an event.

Parameters:

provider: the MenuProvider to be added
owner: the Lifecycle owner whose state will determine the removal of the provider

public void addMenuProvider(MenuProvider provider, LifecycleOwner owner, Lifecycle.State state)

Adds the given MenuProvider to this MenuHost once the given LifecycleOwner reaches the given . This MenuProvider will be removed once the given LifecycleOwner goes down from the given .

Parameters:

provider: the MenuProvider to be added
state: the Lifecycle.State to check for automated addition/removal
owner: the Lifecycle owner whose state will be used for automated addition/removal

public void removeMenuProvider(MenuProvider provider)

Removes the given MenuProvider from this MenuHost.

Parameters:

provider: the MenuProvider to be removed

public void invalidateMenu()

Invalidates the to ensure that what is displayed matches the current internal state of the menu. This should be called whenever the state of the menu is changed, such as items being removed or disabled based on some user event.

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.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

/**
 * A class that allows you to host and
 * keep track of {@link MenuProvider}s that will supply
 * {@link android.view.MenuItem}s to the app bar.
 *
 * @see MenuHostHelper
 */
public interface MenuHost {

    /**
     * Adds the given {@link MenuProvider} to this {@link MenuHost}.
     *
     * If using this method, you must manually remove the provider when necessary.
     *
     * @param provider the MenuProvider to be added
     * @see #removeMenuProvider(MenuProvider)
     */
    void addMenuProvider(@NonNull MenuProvider provider);

    /**
     * Adds the given {@link MenuProvider} to this {@link MenuHost}.
     *
     * This {@link MenuProvider} will be removed once the given {@link LifecycleOwner}
     * receives an {@link Lifecycle.Event.ON_DESTROY} event.
     *
     * @param provider the MenuProvider to be added
     * @param owner    the Lifecycle owner whose state will determine the removal of the provider
     */
    void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner);

    /**
     * Adds the given {@link MenuProvider} to this {@link MenuHost} once the given
     * {@link LifecycleOwner} reaches the given {@link Lifecycle.State}.
     *
     * This {@link MenuProvider} will be removed once the given {@link LifecycleOwner}
     * goes down from the given {@link Lifecycle.State}.
     *
     * @param provider the MenuProvider to be added
     * @param state    the Lifecycle.State to check for automated addition/removal
     * @param owner    the Lifecycle owner whose state will be used for automated addition/removal
     */
    @SuppressLint("LambdaLast")
    void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner,
            @NonNull Lifecycle.State state);

    /**
     * Removes the given {@link MenuProvider} from this {@link MenuHost}.
     *
     * @param provider the MenuProvider to be removed
     */
    void removeMenuProvider(@NonNull MenuProvider provider);

    /**
     * Invalidates the {@link android.view.Menu} to ensure that what is
     * displayed matches the current internal state of the menu.
     *
     * This should be called whenever the state of the menu is changed,
     * such as items being removed or disabled based on some user event.
     */
    void invalidateMenu();
}