. Advertisement .
..3..
. Advertisement .
..4..
The error: “setOnNavigationItemSelectedListener is deprecated” is a common error that can show up in many ways. In this blog, we will go through some of the ways you can fix this issue. Read on.
When Does The Error “setOnNavigationItemSelectedListener is deprecated” Happen?
When utilizing setOnNavigationItemSelectedListener() in your app, you may encounter this issue.
'setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView.OnNavigationItemSelectedListener)' is deprecated
How To Solve The Error: “setOnNavigationItemSelectedListener is deprecated”
Approach 1: Utilize NavigationBarView.setOnItemSelectedListener from its base class
From its base class, NavigationBarView.setOnItemSelectedListener can be used to solve the error ”setOnNavigationItemSelectedListener is deprecated”. Look at this example to further understand about this method:
/**
* 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;
}
After you do that, your error completely resolved.
Approach 2: Utilize setonItemSelectedListener
Another solution for your error is utilizing setonItemSelectedListener.
SetonItemSelectedListener can be used to fix this issue. It has the same functionality as setOnNavigationItemSelectedListener().
bnv.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch(id){
//check id
}
return true;
}
In Kotlin, you may utilize things like:
});
bnv.setOnItemSelectedListener { item ->
when (item.itemId) {
}
true
}
Approach 3: Replace setOnNavigationItemReselectedListener with setOnItemReselectedListener
Excepting two approaches mentioned above, there is another way to fix your error “setOnNavigationItemSelectedListener is deprecated”. This method is replacing setOnNavigationItemReselectedListener with setOnItemReselectedListener and setOnNavigationItemSelectedListener with setOnItemSelectedListener. Let’s consider the following example to understand more about this solution:
navView.setOnItemSelectedListener {
// let's do something now
true
}
// If the default menu can be the first menu
// Should set the default chosen menu BETWEEN setOnItemSelectedListener and setOnItemReselectedListener.
// It will make setOnItemSelectedListener fired when you activate app.
// In the case you set default menu AFTER setOnItemReselectedListener.
// Then setOnItemReselectedListener will fired when you activate app
navView.selectedItemId = R.id.navigation_home
navView.setOnItemReselectedListener {
// let's do something now
}
This method is very simple, isn’t it? However, it has enormous efficiency. It will give you amazing result and help your program run well. So, what are you waiting without doing it to solve your problem? Let’s apply this method to get your desired result.
Conclusion
We hope you enjoy our article about the error. With this knowledge, we know that you can fix your error: “setOnNavigationItemSelectedListener is deprecated” quickly by following these steps! If you still have any other questions about fixing this syntax error, please leave a comment below. Thank you for reading!
Leave a comment