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 Android Data Persistence Key-Value Saving with SharedPreferences Adding the Settings Page

Raghda Talaa't
Raghda Talaa't
2,944 Points

Why choosing the Storage Type from the settings icon is not working?

At this point everything is working except that it's always being saved as Internal (the default) even when i change the settings to save it as Public External

Evan Anger
Evan Anger
Treehouse Guest Teacher

Sorry about that let me take a quick look at this one.

2 Answers

Raghda Talaa't
Raghda Talaa't
2,944 Points

I solved it by adding the onSharedPreferenceChanged as described here:

http://developer.android.com/guide/topics/ui/settings.html#Listening

Luke Liem
Luke Liem
6,367 Points

There is a bug in Evan's code. In MemeMakerApplicationSettings.java, he defined the Preference as "Storage":

package com.teamtreehouse.mememaker;

import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager;

import com.teamtreehouse.mememaker.utils.StorageType;

/**

  • Created by Evan Anger on 8/13/14. */ public class MemeMakerApplicationSettings {

    SharedPreferences mSharedPreferences;

    public MemeMakerApplicationSettings(Context context) { mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); }

    public String getStoragePreference() { // Retrieve Storage Preference with Internal Storage as default return mSharedPreferences.getString("Storage", StorageType.INTERNAL); }

    public void setSharedPreference(String storageType){ mSharedPreferences.edit() .putString("Storage", storageType) .apply(); // apply() is asynchronous, versus commit() being synchronous } }

But in preference.xml and strings.xml, the preference is named "STORAGE" all-cap. This is why the app always default back to INTERNAL.

<?xml version="1.0" encoding="utf-8"?> <resources>

...

<string name="storage_type">STORAGE</string>

</resources>