setOnNavigationItemSelectedListener deprecated

We Are Going To Discuss About setOnNavigationItemSelectedListener deprecated. So lets Start this Java Article.

setOnNavigationItemSelectedListener deprecated

  1. setOnNavigationItemSelectedListener deprecated

    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; } });

  2. setOnNavigationItemSelectedListener deprecated

    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; } });

Solution 1

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.

Advertisements

Original Author Of This Content

Solution 2

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
        }
Advertisements

Original Author Of This Content

Solution 3

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
}
Advertisements

Original Author Of This Content

Solution 4

 viewBindingMainActivity.navView.setOnItemSelectedListener { menuItem ->
            if (menuItem.itemId != R.id.navigation_home) {
                Add your code
                false
            } else {
                Add your code
                true
            }
        }
Advertisements

Original Author Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment