0

Here's where I store values into SharedPreferences in one activity:

        sharedPref = context.getSharedPreferences("sharedPref", Context.MODE_PRIVATE);
        String firstPlace = new String("1");
        String secondPlace = new String("2");
        String thirdPlace = new String("3");

        editor = sharedPref.edit();

        editor.putString("first", firstPlace);
        editor.putString("second", secondPlace);
        editor.putString("third", thirdPlace);
        editor.commit();

And try to retrieve them in another activity. However, the retrieve doesn't seem to be getting the values I put in and is just using the defaults (so "1st Place: " "2nd Place: " and "3rd Place: " end up with a 'no' next to them).

SharedPreferences sharedPref = getSharedPreferences("sharedPref", MODE_PRIVATE);

    String firstPlace = sharedPref.getString("first", "no");
    String secondPlace = sharedPref.getString("second", "no");
    String thirdlace = sharedPref.getString("third", "no");

    highScore1.setText("1st Place: " + firstPlace);
    highScore2.setText("2nd Place: " + secondPlace);
    highScore3.setText("3rd Place: " + thirdlace);
Freckles
  • 5
  • 4
  • Can you also add the code where Editor object is created? – Sanoop Surendran May 10 '16 at 04:23
  • Try `.apply()` than `.commit()` – Sanoop Surendran May 10 '16 at 04:24
  • That's pretty much all I do with the editor... I have it as a global variable at the top " "SharedPreferences.Editor editor;" and then I do the "editor = sharedPref.edit();" as you see in the first block of code. Is there more I have to do to it? – Freckles May 10 '16 at 04:28
  • You seem to be using different contexts. Not sure if that matters, though – OneCricketeer May 10 '16 at 04:29
  • @Freckles your code seems fine. If by some reason you are unable to retrieve the value, I would see whether they are saved in the actual xml file which can be found in /root/data/data/your.app.package/shared_pref/sharedPref.xml (note that this requires root). Alternatively, you can use my [SharedPreferences library](https://github.com/hendraanggrian/ORMPreferences) which could ease up things for you. – Hendra Anggrian May 10 '16 at 04:35
  • Try [this](http://stackoverflow.com/a/36856455/5733111) .. By doing so it will ease the LOC – Sanoop Surendran May 10 '16 at 04:35
  • @Hendra Anggrian thanks! I guess I can't do that in looking at the project folder structure on the side of Android Studio, huh. How do I access that path? Command line? Sorry, new to all of this ^_^; – Freckles May 10 '16 at 04:56
  • did your problem solved or not – Muhammad Younas May 10 '16 at 05:09
  • @Freckles to access that directory, you would need your Android device to be rooted. If you are unwilling to root your device then you could use pre-rooted emulator like Genymotion. Once rooted, you may access it by command line or third-party file manager app that supports root access like ES File Explorer. – Hendra Anggrian May 10 '16 at 05:21

3 Answers3

0

You can try to direct put value

    editor = sharedPref.edit();
    editor.putString("first", "1");
    editor.putString("second", "2");
    editor.putString("third", "3");
Rathod Vijay
  • 417
  • 2
  • 7
0

You have used separate contexts. To make your sharedpreference accesible to entire application you need default sharedpreference. Declare it as below

   SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

After this you can edit and fetch data as you did before.

Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
0

please try this,

this.SharedPrefs = getSharedPreferences("MyPrefs", 0); SharedPreferences.Editor localEditor = this.SharedPrefs.edit(); localEditor.putString("first", firstPlace); localEditor.putString("second", secondPlace); localEditor.putString("third", thirdPlace); localEditor.commit(); finish();