Search code examples
androidandroid-navigation

BottomNavigation View OnNavigationItemSelectedListener is deprecated


I am trying out the Android's BottomNavigationView implementation as per Material Design

However, on the MainActivity code I am getting a warning that OnNavigationItemSelectedListener is deprecated - see the below snapshot

enter image description here

Have tried get an alternative method to work with the BottomNavigationView but I cannot find it.

Looking for help from anyone with a way out but in the meantime I have matched my BottomView's menu items ids with the fragment destination ids and I successfully achieved Navigation but with a limitation of not being able to update my toolbar title with the Fragment's name.


Solution

  • Just use the OnItemSelectedListener interface:

    kotlin

    bottomNavigationView?.setOnItemSelectedListener { item ->
        // do stuff
    
        return@setOnItemSelectedListener true
    }
    

    Java

    bottomNavigationView.setOnItemSelectedListener(item -> {
        // do stuff
    
        return true;
    });