opike
opike

Reputation: 7595

Issue reading preferences in android activity

I'm trying to use the PreferenceActivity in a java app and I'm able to bring up a screen to modify the preferences but I'm unable to read the preferences in a nother activity in the same App.

I have one xml file under the xml/ folder called default_values.xml that contains just a single preference.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">


<EditTextPreference
        android:key="json_url_pref"
        android:defaultValue="@string/json_url"
        android:title="@string/title_edittext_preference"
        android:summary="@string/summary_edittext_preference"
        android:dialogTitle="@string/dialog_title_edittext_preference" />


</PreferenceScreen>

This is the code I'm trying to use in my Activity to read the preference:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(EarningsActivity.this);
String tmp = sp.getString("json_url_pref","-1");

sp.getString(...) is always returning -1.

I think this is a contextual issue that for some reason this activity does not have access to the application preferences (at least my intention is to have the preferences be visible by all activities in the application).

Upvotes: 1

Views: 1985

Answers (1)

svdree
svdree

Reputation: 13348

The context that you're passing to getDefaultSharedPreferences() should be in the same package as the PreferenceActivity subclass that handles your preferences. Is that the case in your code?

Upvotes: 2

Related Questions