setOnNavigationItemSelectedListener deprecated
Asked Answered
E

10

58

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.

Ergener answered 17/6, 2021 at 14:54 Comment(1)
its not deprecated developer.android.com/reference/com/google/android/material/…Forward
Y
74

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.

Yuji answered 17/6, 2021 at 17:54 Comment(2)
I tried it. it works fine, Thanks. but the speed of switching between nav items gets a bit slower than OnNavigationItemSelectedListener. any suggestions to improve it? Anyways thanks for the helpErgener
maybe add empty listener to setOnItemReselectedListener, this way your listener in setOnItemSelectedListener will not get called when item is re-selectedYuji
D
57

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
        }
Dorser answered 16/7, 2021 at 16:14 Comment(0)
S
6

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
}
Subdeacon answered 8/11, 2021 at 2:5 Comment(0)
M
5

you can use in Kotlin

buttmNav.setOnItemSelectedListener { item ->
        when (item.itemId) {
        }
        true
    }
Moil answered 28/8, 2022 at 20:30 Comment(1)
Why are you using setOnItemSelectedListener in place of setOnNavigationItemSelectedListener?Rabid
U
4
public class HomeActivity extends AppCompatActivity implements NavigationBarView.OnItemSelectedListener {

    BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        bottomNavigationView = findViewById(R.id.bottom_nav);
        bottomNavigationView.setOnItemSelectedListener(this);
        displayfragment(new FragmentHome());

    }

    private void displayfragment(Fragment fragment) {

        getSupportFragmentManager().beginTransaction().replace(R.id.content_area, fragment).commit();


    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment;
        switch (item.getItemId()) {

            case R.id.nav_home:
                fragment = new FragmentHome();
                break;
            case R.id.nav_fav:
                fragment = new FavouriteFragment();
                break;
            case R.id.nav_set:
                fragment = new FragmentSetting();
                break;

            default:
                fragment = new FragmentHome();
                break;

        }
        displayfragment(fragment);
        return true;
    }
}
Ureide answered 6/2, 2023 at 11:35 Comment(2)
Please explain what your solution is.Rabid
setOnNavigationItemSelectedListener is deprecated use this instead setOnItemSelectedListener(this);Ureide
W
1

kotlin: use setOnItemSelectedListener

    bottomNavigationView.setOnItemSelectedListener { item: MenuItem ->
            when (item.itemId) {
                 R.id. ... -> {
                    Add your code
                 true
            }
            
            else ->
                true
        }
Wadlinger answered 19/6, 2022 at 13:38 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Malina
F
1

Now You Should Use this

**`setOnItemSelectedListener`**
Favoritism answered 21/1 at 13:32 Comment(0)
R
1

bottomNavigationView.setOnNavigationItemSelectedListener is deprecated,

You can use as below in java,

bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener()
        {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item)
            {
                
            }
        });

For Checking Reselected Items use setOnItemReselectedListener

bottomNavigationView.setOnItemReselectedListener(new NavigationBarView.OnItemReselectedListener()
        {
            @Override
            public void onNavigationItemReselected(@NonNull MenuItem item)
            {
                switch (item.getItemId())
                {
                    
                }
            }
        });
Radial answered 8/5 at 7:26 Comment(0)
M
0

 viewBindingMainActivity.navView.setOnItemSelectedListener { menuItem ->
            if (menuItem.itemId != R.id.navigation_home) {
                Add your code
                false
            } else {
                Add your code
                true
            }
        }
Mineraloid answered 25/7, 2021 at 0:45 Comment(0)
D
-1

Add below line in your code @SuppressWarnings("deprecation")

Duckpin answered 10/7, 2023 at 11:20 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Malina

© 2022 - 2024 — McMap. All rights reserved.