Asaak
Asaak

Reputation: 597

SharedPreferences variable always returns false

I am using a Preference Fragments following the step by step guide on Android guide.

I am trying to set some preferences using this fragment, so later on the code I can check the value of each variable to perform actions accordingle.

Mi Preference fragment is working fine. However, when I try to recover the value of a CheckedBoxPreference somewhere else on the code, it always returns false.

This is the preference xml file :

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:persistent="true"
    >
    <PreferenceCategory
        android:title="NOTIFICACIONES"
        android:key="pref_key_storage_settings">

    <CheckBoxPreference
        android:key="sendMail"
        android:defaultValue="false"
        android:persistent="true"
        android:summary="Send emails to contacts"
        android:title="E-mail"/>
</PreferenceCategory>
</PreferenceScreen>

This is the class I've done to use SharedPReferences

public class Prefs{
    private static Prefs myPreference;
    private SharedPreferences sharedPreferences;
    private static final String NOMBRE_PREFERENCIAS = "MisPreferencias";

    public static Prefs getInstance(Context context) {
        if (myPreference == null) {
            myPreference = new Prefs(context);
        }
        return myPreference;
    }

    private Prefs(Context context) {

        sharedPreferences = context.getSharedPreferences(NOMBRE_PREFERENCIAS,context.MODE_PRIVATE);
    }

 public Boolean getBoolean(String key)
    {
        boolean returned = false;

        if (sharedPreferences!= null) {
            returned = sharedPreferences.getBoolean(key,false);
        }

        return returned;
    }
}

And this is how I check if the options is selected, so I can send/or not emails to clients

Prefs preferences = Prefs.getInstance(getApplicationContext());
        if(preferences.getBoolean("sendMail")
        {
          // .... send email   
        }

As I said, what is strange is that It is persistent on the settings fragment ( if I select the option sendEmmail, it is selected even if I close the app and reopen it. However, when I check the value using my method, it always returns false.

What am I doing wrong?

Thank you

Upvotes: 1

Views: 1004

Answers (1)

Szymon
Szymon

Reputation: 43023

Since you are using the preference fragment, you should be using PreferenceManager.getDefaultSharedPreferences(android.content.Context) to retrieve your preferences. You're using named preferences at the moment.

From developer.android.com on PreferenceFragment:

Shows a hierarchy of Preference objects as lists. These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this fragment will use, call getDefaultSharedPreferences(android.content.Context) with a context in the same package as this fragment.

So the line

sharedPreferences = context.getSharedPreferences(NOMBRE_PREFERENCIAS,context.MODE_PRIVATE);

should be replaced with:

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

Upvotes: 3

Related Questions