HeartDeath
HeartDeath

Reputation: 79

How saving the state of a switch

I am developing a game on Android, I have a background music that stop with a switch and that is in an other activity, that of the parameters. The music is in the main menu.

I would like to know how to save the state of the switch (checked or not checked) by going from the main menu to the parameters

This the code for my switch :

buttonmusique = (Switch) findViewById(R.id.switchMusique);
   buttonmusique.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.e("bouton", String.valueOf(buttonmusique.isChecked()));
                // If the music is playing
                if (isChecked) {
                    Intent music = new Intent();
                    music.setClass(Parametres.this, BackgroundMusic.class);
                    startService(music);
                    musicBoolean = true;
                    buttonmusique.setText("Musique On");  //To change the text near to switch
                    Log.d("You are :", "Checked");
                } else {
                    Intent music = new Intent();
                    music.setClass(Parametres.this, BackgroundMusic.class);
                    stopService(music);
                    musicBoolean = false;
                    // Resume the music player
                buttonmusique.setText("Musique OFF");   //To change the text near to switch
                Log.d("You are :", " Not Checked");

            }
        }
    });

Upvotes: 2

Views: 168

Answers (4)

EtherPaul
EtherPaul

Reputation: 423

Put it in your create method: (+adapt to your situation)

final SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
CheckBox checkbox = (CheckBox) findViewById(R.id.myCheckBox);
checkbox.setChecked(sharedPreferences.getBoolean("mycheckbox", true));

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                sharedPreferences.edit().putBoolean("mycheckbox", b).apply();
            }
        });

Upvotes: 2

Andrej Jurkin
Andrej Jurkin

Reputation: 2266

You can save it in SharedPreferences.

SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);

Save sound settings

SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(getString(R.string.sound_enabled), isChecked);
editor.commit();

To get sound settings use:

boolean soundEnabled = prefs.getBoolean(getString(R.string.sound_enabled), false);

// Set switch state
buttonmusique.setChecked(soundEnabled);

Upvotes: 2

W4R10CK
W4R10CK

Reputation: 5550

Use shared preferences or a database to store the state of your switch. It is essential that you depend on the lifecycle methods of Activity/fragment.

The following might help you:

......
.....
@Override
public void onClick(View v) 
{
    if (toggle.isChecked()) 
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
        editor.putBoolean("state_to_save", true);
        editor.commit();
    }
    else
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
        editor.putBoolean("state_to_save", false);
        editor.commit();
    }
}

The final Code:

@Override
protected void onCreate(Bundle savedInstanceState) 
{ 
 .......
    ......
    SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyle", MODE_PRIVATE);
    toggle.setChecked(sharedPrefs.getBoolean("state_to_save", true));
}

Upvotes: 1

Beppi's
Beppi's

Reputation: 2129

Load status:

boolean switch_status = PreferenceManager.getDefaultSharedPreferences(ctx).getBoolean("switch_status", false);

Save status:

SharedPreferences.Editor sped = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
    sped.putBoolean("switch_status", value);
    sped.commit();

Upvotes: 1

Related Questions