In my application, I use a checkBox that when it is checked, a piece of music is played. But when the user moves to another activity and then returns to the activity that the checkBox is there, it is unchecked. In order to stop the music, you have to check the checkBox again and after that uncheck it. How can I save the state of checkBox unchanged as I move between activities?
This is my XML code:
<CheckBox
android:id="@+id/chk_box_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="4dp"
android:layout_marginLeft="12dp"
android:layout_gravity="center"
android:layout_marginStart="12dp"/>
This is my JAVA code:
CheckBox checkBox = (CheckBox) findViewById(R.id.chk_box_music);
checkBox.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener({
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b == true){
startService(new Intent(SettingsActivity.this, MyService.class));
}else{ stopService(new Intent(SettingsActivity.this, MyService.class));
}
}
});