1

So I'm trying to save a value to sharedpreferences by a click of a button, and then see which value it is in another activity. (to basically set a background for activity2 based on which button they pressed in activity1)

Saving code:

    public void onClick(View v) {
    SharedPreferences.Editor background = getSharedPreferences("Background", MODE_PRIVATE).edit();
    if(btn1 == v)
    {
        background.remove("selectedBG");
        Toast.makeText(this, "btn1", Toast.LENGTH_SHORT).show();
        background.putInt("selectedBG", 1);
        background.commit();
    }
    if(btn2 == v)
    {
        background.remove("selectedBG");
        background.putInt("selectedBG", 2);
        Toast.makeText(this, "btn2", Toast.LENGTH_SHORT).show();
        background.commit();
    }
    if(btn3 == v)
    {
        background.remove("selectedBG");
        background.putInt("selectedBG", 3);
        Toast.makeText(this, "btn3", Toast.LENGTH_SHORT).show();
        background.commit();
    }
    if(btn4 == v)
    {
        background.remove("selectedBG");
        background.putInt("selectedBG", 4);
        Toast.makeText(this, "btn4", Toast.LENGTH_SHORT).show();
        background.commit();
    }

}

And then, the Toast here always shows "chosenbackground:0":

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.play);
    LLayout=(LinearLayout)findViewById(R.id.llayout);
    SharedPreferences background2 = getSharedPreferences("Background", MODE_PRIVATE);
    int chosenBackground = background2.getInt("selectedBg", 0);
    Toast.makeText(this,"chosenBackground:" + chosenBackground, Toast.LENGTH_SHORT).show();
    if (chosenBackground != 0) {
        if(chosenBackground==1)
        {
            LLayout.setBackgroundColor(Color.WHITE);
        }
        if(chosenBackground==2)
        {
            LLayout.setBackgroundColor(Color.rgb(34,34,34));
        }
        if(chosenBackground==3)
        {
            LLayout.setBackgroundColor(Color.rgb(51,68,85));
        }
        if(chosenBackground==4)
        {
            LLayout.setBackgroundColor(Color.rgb(68,34,17));
        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Tom
  • 15
  • 7

3 Answers3

2

Answer for your question is that you have misspelled the key in second activity, in first one you are using "selectedBG" but in the second one "selectedBg". It is not the same, it's case sensitive. Correct in the second one for "selectedBG" and it should work.

b2mob
  • 3,419
  • 2
  • 14
  • 22
  • Thank you very much, I did not quite understand (1), (apart from the using else if instead of if, which I will do so). Is it possible for you to show me the new code? – Tom Mar 18 '17 at 12:56
  • Sadly that does not work, the Toast I mentioned in the original post still states 0, and does not change the background. – Tom Mar 18 '17 at 14:41
  • Oh my god... 8 hours and I didn't see that. thank you. – Tom Mar 18 '17 at 17:20
2

Using the SharedPreferences here it's really bad idea, if u only want to pass a background or rather a color if I see it correctly. Just pass it in intent:

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("EXTRA_BACKGROUND_ID", background);
startActivity(intent);

Access that intent on next activity for eg. in onCreate()

String s = getIntent().getStringExtra("EXTRA_SESSION_ID"); 

@Updated

public class PreferencesUtils {

    private SharedPreferences sharedPrefs;
    private SharedPreferences.Editor prefsEditor;

    public static final String KEY_BACKGROUND = "BACKGROUND";

    public PreferencesUtils(Context context) {
        this(context, PREFS_DEFAULT);
    }

    public PreferencesUtils(Context context, String prefs) {
        this.sharedPrefs = context.getSharedPreferences(prefs, Activity.MODE_PRIVATE);
        this.prefsEditor = sharedPrefs.edit();
    }

    public int getValue(String key, int defaultValue){
        return sharedPrefs.getInt(key, defaultValue);
    }

    public boolean saveValue(String key, int value){
        prefsEditor.putInt(key, value);
        return prefsEditor.commit();
    }
}

PreferencesUtils preferencesUtils = new PreferencesUtils(this);
preferencesUtils.saveValue(PreferencesUtils.KEY_BACKGROUND, 1); //saveValue

preferencesUtils.getValue(PreferencesUtils.KEY_BACKGROUND, 0); //getValue, 

second arg is defult if not found

Minis
  • 463
  • 3
  • 10
  • I want it to save for future reference though, so they choose once and it will stay that way no matter if they restarted the game. – Tom Mar 18 '17 at 12:53
  • http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Minis Mar 18 '17 at 12:57
  • That is exactly what I used, but in my case it simply doesn't work. – Tom Mar 18 '17 at 13:15
1

Use if (!background2.contains("selectedBg")) to check ,first whether the key exists and if not getInt is not able to create a key and hence always returns default value 0.Also you can use apply() instead of commit to check whether commit has taken place successfully.Debug the code more to see all possibilities

int chosenBackground=0;
if (!background2.contains("selectedBg"))
    {
        //is called once when  after you freshly install the app
        background2.putInt("selectedBG", 0);
    }

    else
    chosenBackground = background2.getInt("selectedBg", 0);
OutOfBounds 94
  • 77
  • 1
  • 13
  • You are right, it doesnt contain a key. How come? I obviously assiged it a key earlier on. – Tom Mar 18 '17 at 14:57