setOnNavigationItemSelectedListener deprecated

Jeyasurya picture Jeyasurya · Jun 17, 2021 · Viewed 19.8k times · Source

Currently I am working on an app which has a bottom navbar with three menu items. I had used setOnNavigationItemSelectedListener() for items being clicked. but now iam facing issue that the method has been depreciated.

  • App Language: Java
  • Issue: 'setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener)' is deprecated

Is there any way to resolve it? is is there any better alternative than setOnNavigationItemSelectedListener() method.

Answer

marcinj picture marcinj · Jun 17, 2021

Its deprecated according to github sources: BottomNavigationView.setOnNavigationItemSelectedListener

In its comment you can read:

@deprecated Use {@link NavigationBarView#setOnItemSelectedListener(OnItemSelectedListener)}
   *     instead.

so use NavigationBarView.setOnItemSelectedListener from its base class:

  /**
   * Set a listener that will be notified when a navigation item is selected. This listener will
   * also be notified when the currently selected item is reselected, unless an {@link
   * OnItemReselectedListener} has also been set.
   *
   * @param listener The listener to notify
   * @see #setOnItemReselectedListener(OnItemReselectedListener)
   */
  public void setOnItemSelectedListener(@Nullable OnItemSelectedListener listener) {
    selectedListener = listener;
  }

Also see this commit

as it explains confusion about this change:

The listeners were deprecated in favor of NavigationBarView#OnItemSelectedListener and NavigationBarView#OnItemReselectedListener, but deprecation documentation was never added, so it's unclear what developers should use instead.