You have a bug in the condition check:
if (on = true) {
tb.setChecked(true);
} else {
tb.setChecked(false);
}
In Java, the comparison is done with ==
- in your code, value of on is set to true
in the condition part of the if, so onCreate always sets the ToggleButton
checked.
As a side note, it is a bad habit to compare a boolean to true - it is generally more readable code if you simply test if (on)
. However in your case this test is not even needed - you can simply replace the block with tb.setChecked(on)