Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Josh Gold
Josh Gold
12,207 Points

Setting Shared Preferences works in one Java class, but not in another Java class.

I can successfully update the shared preferences in one Java class, but not in the other. The code appears to be identical though.

First the code that is NOT successfully setting Shared Preferences:

        View.OnClickListener eraseAppSettings = new         View.OnClickListener() {
            @Override
            public void onClick(View view) {

                rank = 0;
                piecesOfEight = 0;
                wordIndex = 0;

                getPreferences(MODE_PRIVATE).edit().putInt("UserRank", rank).commit();
                getPreferences(MODE_PRIVATE).edit().putInt("UserPoints", piecesOfEight).commit();
                getPreferences(MODE_PRIVATE).edit().putInt("WordIndex", wordIndex).commit();

                Toast.makeText(SettingsActivity.this, "Yer settings be erased Matey", Toast.LENGTH_LONG).show();
            }
        };

Then the code that is successfully setting Shared Preferences:

                getPreferences(MODE_PRIVATE).edit().putInt("UserRank", rank).commit();
                getPreferences(MODE_PRIVATE).edit().putInt("UserPoints", piecesOfEight).commit();
                getPreferences(MODE_PRIVATE).edit().putInt("WordIndex", wordIndex).commit();

Looks pretty much the same to me. In fact I have copy and pasted the code.

Perhaps in one of the activities it is reading a different preferences file? Here is the code I use to retrieve/read preferences:

        rank = getPreferences(MODE_PRIVATE).getInt("UserRank", rank);
        piecesOfEight = getPreferences(MODE_PRIVATE).getInt("UserPoints", piecesOfEight);
        wordIndex = getPreferences(MODE_PRIVATE).getInt("WordIndex", wordIndex);

2 Answers

Hello,

I very well could be wrong, but according to this, using getPreferences will return preferences associated only with that activity. If you're wanting to use the same preferences in different activities, you might want to look at this posting at stackoverflow.

Josh Gold
Josh Gold
12,207 Points

James, that sounds reasonable. I am going to create method that clears prefences inside the same activity, where things are already working. Then I will call that clear method, from the other activity.

I'd recommend just using the default shared preference manager for the application's context like the second link I gave outlined.

Josh Gold
Josh Gold
12,207 Points

Hello James, I did what you suggested. Here are the pieces of code I used throughout my program that works.

               //get prefs
                rank = LoadPreferences("UserRank", rank);
                piecesOfEight = LoadPreferences ("UserPoints", piecesOfEight);

                //save prefs
                savePrefs("UserPoints", piecesOfEight);
                savePrefs("UserRank", rank);

    //save prefs
    public void savePrefs(String key, int value){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    //get prefs
    private int LoadPreferences(String key, int value){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        int data = sharedPreferences.getInt(key, value);
        return data;
    }