MaterialButtonToggleGroup addOnButtonCheckedListener called twice every time
00:08 18 Jun 2023

For some bizarre reason, addOnButtonCheckedListener is called twice every time I click any of the buttons inside MaterialButtonToggleGroup. Below is my XML


    
        
            
            
        
    

and below is the code

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.btgTest.addOnButtonCheckedListener { group, checkedId, isChecked ->
            Timber.d("imini 177 isChecked: $isChecked")
            if (isChecked) {
                when (checkedId){
                    R.id.btnTest1 -> Timber.d("1 is checked")
                    R.id.btnTest2 -> Timber.d("2 is checked")
                }
            }else{
                Timber.d("imini 184")
            }
        }
    }

Every time I click the top button btnTest1 my logcat will output

imini 177 isChecked: true
1 is checked
imini 177 isChecked: false
imini 184

and every time I click the bottom button btnTest2 my logcat will output

imini 177 isChecked: false
imini 184
imini 177 isChecked: true
2 is checked

So as you can see, not only addOnButtonCheckedListener called twice every time I click on any buttons inside it, but when I click on the top button, it will always somehoow marked as not checked. Why?

android kotlin