Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnNavigationItemSelectedListener deprecated

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.

like image 701
Jeyasurya Avatar asked Jun 17 '21 14:06

Jeyasurya


Video Answer


4 Answers

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.

like image 178
marcinj Avatar answered Oct 23 '22 13:10

marcinj


you can try setonItemSelectedListener. It is working same as setOnNavigationItemSelectedListener()[tested in android 11]

bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            switch(id){
              //check id
            }
            return true;
        }
    });

Kotlin:

bnv.setOnItemSelectedListener { item ->
            when (item.itemId) {
            }
            true
        }
like image 21
Unknown2433 Avatar answered Oct 23 '22 14:10

Unknown2433


We can use setOnItemSelectedListener instead of setOnNavigationItemSelectedListener and setOnItemReselectedListener instead of setOnNavigationItemReselectedListener

navView.setOnItemSelectedListener {
    // do something
    true
}

// In case the default menu can be the first menu 
// Should set the default selected menu BETWEEN setOnItemSelectedListener and setOnItemReselectedListener. 
// It will make setOnItemSelectedListener fired when you launch app. 

// If you set default menu AFTER setOnItemReselectedListener. 
// Then setOnItemReselectedListener will fired when you launch app
navView.selectedItemId = R.id.navigation_home


navView.setOnItemReselectedListener {
    // do something
}
like image 5
Linh Avatar answered Oct 23 '22 15:10

Linh


you can use in Kotlin

buttmNav.setOnItemSelectedListener { item ->
        when (item.itemId) {
        }
        true
    }
like image 1
devio Avatar answered Oct 23 '22 13:10

devio