简体   繁体   中英

Bottom navigation icon only changing color when clicked twice?

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM