1

When I click on an icon it doesn't change color unless I click it again, it goes to the activity but only the first icon remains highlighted. I have to click it again for the icon to change color. What is wrong with my code?

class ProfileActivity : BaseActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)

        val navigationBar = findViewById<BottomNavigationView>(R.id.navigation_bar)
        navigationBar.setOnNavigationItemSelectedListener(navigation_bar)

    }
private val navigation_bar = BottomNavigationView.OnNavigationItemSelectedListener { item ->

    when (item.itemId) {
        R.id.nav_profile -> {
//            startActivity(Intent(this@ProfileActivity, ProfileActivity::class.java))
            return@OnNavigationItemSelectedListener true
        }

        R.id.nav_explore -> {
            startActivity(Intent(this@ProfileActivity, ExploreActivity::class.java))
           return@OnNavigationItemSelectedListener true
        }

        R.id.nav_store -> {
            startActivity(Intent(this@ProfileActivity, StoreActivity::class.java))
            return@OnNavigationItemSelectedListener true
        }

        R.id.nav_board -> {
            startActivity(Intent(this@ProfileActivity, BoardActivity::class.java))
           return@OnNavigationItemSelectedListener true
        }

    }
    false
} }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
redcactus
  • 29
  • 1
  • 2
  • Use fragments. It's better and well suited to BottomNavigation. Everything you can do in activity, you can do in fragment too. – Basu Jul 23 '20 at 15:46

2 Answers2

1

You are using activity. use Fragment. i think it will be solved.

Jewel Rana
  • 2,397
  • 1
  • 19
  • 28
1

You are using different activities for each item.
You have to handle the selected items in your activities with something like:

navigationBar.setSelectedItemId(R.id.nav_explore)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841