如何在Android上保存preferenceActivity状态

时间:2015-05-06 19:57:24

标签: android sharedpreferences preferenceactivity

我正在尝试开发一个具有PreferenceActivity两个CheckBoxPreference和MainActivity的应用程序。我希望你选择的时候,其中一些人会去MainActivity,保存,她再次选择只看到你先选择了。我尝试了所有的东西但没有任何效果帮我!! 我的代码:

public class Preferencias extends PreferenceActivity implements OnPreferenceChangeListener{
Boolean bmusica=true,bvibracion=true;

SharedPreferences prefs;
CheckBoxPreference chMusica,chVibr;
SharedPreferences.Editor gg;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferencias);

    if(savedInstanceState!=null){

        chMusica.setChecked(savedInstanceState.getBoolean("estadoMusica"));
        bmusica=savedInstanceState.getBoolean("valorMusica");
        chVibr.setChecked(savedInstanceState.getBoolean("estadoVibracion"));
        bvibracion=savedInstanceState.getBoolean("valorVibr");

    }else{


     prefs =this.getPreferenceManager().getSharedPreferences();
     gg= prefs.edit();
     chMusica = (CheckBoxPreference) findPreference("musica");
     chVibr = (CheckBoxPreference) findPreference("vibracion");
     chMusica.setChecked(true);
     chVibr.setChecked(true);
    } 


}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {

    if(chMusica.isChecked()){
        gg.putBoolean("musica", true);
        bmusica=true;
    }else{
        gg.putBoolean("musica", false);
        bmusica=false;
    }
    if(chVibr.isChecked()){
        gg.putBoolean("vibracion", true);
        bvibracion=true;
    }else{
        gg.putBoolean("vibracion", false);
        bvibracion=false;
    }

    return true;
}
@Override
protected void onSaveInstanceState(Bundle outState) {

    outState.putBoolean("estadoMusica", chMusica.isChecked());
    outState.putBoolean("valorMusica", bmusica);
    outState.putBoolean("estadoVibracion", chVibr.isChecked());
    outState.putBoolean("valorVibr", bvibracion);
    super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);

    chMusica.setChecked(state.getBoolean("estadoMusica"));
    chVibr.setChecked(state.getBoolean("estadoVibracion"));
    bmusica=state.getBoolean("valorMusica");
    bvibracion=state.getBoolean("valorVibr");
}

我的MainActivity:

public class MainActivity extends Activity {
Button jugar, preferencias, acercade, maximo, salir;
Vibrator v;
Boolean isVibrar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_land);
    v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
    jugar = (Button) findViewById(R.id.botonJugar);
    preferencias = (Button) findViewById(R.id.botonOpciones);
    acercade = (Button) findViewById(R.id.botonAcercaDe);
    maximo = (Button) findViewById(R.id.botonPuntuaciones);
    salir = (Button) findViewById(R.id.botonSalir);
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    isVibrar = sharedPref.getBoolean("vibracion", true);


public void preferencias(View view) {
    if (isVibrar==true) {
        v.vibrate(200);
    }

    Intent i = new Intent(MainActivity.this, Preferencias.class);
    startActivity(i);
}

0 个答案:

没有答案