Domanda

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib1= (ImageButton) findViewById(R.id.imageButton1);
        getpref();
        ib1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if(sound==true){
                    sound=false;
                    ib1.setImageResource(R.drawable.volume);
                }
                else{
                    sound=true;
                    ib1.setImageResource(R.drawable.vol);
                }
            }
        });
    };
    public void getpref() {
        SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        Boolean sh_sound = prefs.getBoolean("key", true);
        sound=sh_sound;
        if(sh_sound==true){
            SharedPreferences pref = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key", sh_sound);
            editor.commit();
            checkpref(sh_sound);
        }
        else{
            SharedPreferences pref = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key", sh_sound);
            editor.commit();
            checkpref(sh_sound);
        }
    }
    public void checkpref(Boolean sh_sound) {
        if(sh_sound== true){
            ib1.setImageResource(R.drawable.vol);
        }
        else{
            ib1.setImageResource(R.drawable.volume);
        }
    }

I want to save the preference on image click and when I reopen the activity the previous selected choice should be there but I am unable to store the preference, it shows the default image when I reopen.

The default view

After selecting the image to store preference

Reopening the activity

È stato utile?

Soluzione

I did not test the code, but your logic was wrong. You were not saving the preference on click.

Boolean sound;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ib1= (ImageButton) findViewById(R.id.imageButton1);
    getpref();
    ib1.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            if(sound==true){
                sound = false;
                ib1.setImageResource(R.drawable.volume);
            } else{
                sound = true;
                ib1.setImageResource(R.drawable.vol);
            }
            setSoundChecked(sound);
        }
    });
};

void setSoundChecked(Boolean checked) {
    SharedPreferences pref = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    Editor editor = pref.edit();
    editor.putBoolean("key", checked);
    editor.commit();
}

public void getpref() {
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    Boolean sh_sound = prefs.getBoolean("key", true);
    sound = sh_sound;
    checkpref(sh_sound);
}

public void checkpref(Boolean sh_sound) {
    if(sh_sound== true){
        ib1.setImageResource(R.drawable.vol);
    }
    else{
        ib1.setImageResource(R.drawable.volume);
    }
}

Altri suggerimenti

It doesn't look like you are actually trying to save the preference inside the click event. Try updating your click listener like this:

    ib1.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            if(sound==true){
                sound=false;
                ib1.setImageResource(R.drawable.volume);
            }
            else{
                sound=true;
                ib1.setImageResource(R.drawable.vol);
            }

            SharedPreferences pref = getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
            Editor editor = pref.edit();
            editor.putBoolean("key", sound);
            editor.commit();
        }
    });

Other than that, you seem to be saving a value you just read inside your getpref() you could simplify it like this:

public void getpref() {
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    Boolean sh_sound = prefs.getBoolean("key", true);
    sound=sh_sound;       

    checkpref(sh_sound);        
}

Also call getpref() method either on Button onClick as last statement or on Activity onPause/OnStop to save latest value of sh_sound in SharedPreferences as:

@Override
public void onPause()
{
      // call getpref() here..
     getpref();
    super.onPause();

}

You are writing to your prefs in the getprefs() method which is only called when the Activity starts. You should write when you click the Button.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top