I'm building my first Android app, so apologies in advance for my lack of expertise. I've been banging away at this issue for 2 days so far. When I set the sound to mute in my Settings Activity via Shared Preferences, the setting does not 'stick'. Here is the code from my Main Activity:
SharedPreferences settingsSP;
boolean muteSound;
Then in onCreate:
settingsSP = getApplicationContext().getSharedPreferences("PlayerPreferences", Context.MODE_PRIVATE);
muteSound = settingsSP.getBoolean("muteSound", false);
Then I obviously use the muteSound variable to determine whether or not to play sounds within the MainActivity. Now when I go the Settings activity I have this code:
SharedPreferences settingsSP;
boolean muteSound;
Then in onCreate I have this:
settingsSP = getApplicationContext().getSharedPreferences("PlayerPreferences", Context.MODE_PRIVATE);
if (muteSound) {
activitySettingsBinding.displayMuteCheckBox.setChecked(true);
} else {
activitySettingsBinding.displayMuteCheckBox.setChecked(false);
}
activitySettingsBinding.displayMuteCheckBox.setOnClickListener(v -> {
if (activitySettingsBinding.displayMuteCheckBox.isChecked()) {
muteSound = true;
SharedPreferences.Editor editor = settingsSP.edit();
editor.putBoolean("muteSound", muteSound);
editor.apply();
} else {
muteSound = false;
SharedPreferences.Editor editor = settingsSP.edit();
editor.putBoolean("muteSound", muteSound);
editor.apply();
soundPool.play(bubble, 1, 1, 1, 0, 1);
}
The xml code:
<CheckBox
android:id="@+id/displayMuteCheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:gravity="center_vertical"
android:layoutDirection="rtl"
android:paddingLeft="40dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="Mute Sound"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
android:theme="@style/CheckBoxStyle" />
I placed a number of log.d tags throughout the code so that I observe what was happening. For example after I cold booted the app in Android Studio:
- Open MainActivity, onCreate muteSound = false
- Tap Settings button and the Settings onCreate lists muteSound = false
- I check the checkbox to set it to muteSounds = true
- I tap return button within Settings to return to MainActivity
- MainActivity onCreate muteSound = true
- Tap Settings button and the Settings onCreate lists muteSound = false
- And the checkbox is unchecked - because muteSound = false
I can't figure out why the settings is not sticking. When I checked to set the muteSound to be true, that settings is lost when I return to the Settings Activity. I've searched the code to see if I set muteSound = true on accident somewhere else, but when searching the usages I did not find any. Is my assumption about sharePreferences incorrect. It doesn't appear to hold the settings. Or perhaps am I using the checkbox incorrectly, maybe I should be using another type of button here. I would like to use checkboxes to control the display of the score, time and other items on the MainActivity and throughout other app Activities, but if I can't figure this gap in my knowledge I won't be able to. What am I doing incorrectly?
Aucun commentaire:
Enregistrer un commentaire