Are you trying to let the user change the value with a preference activity,
and still get access to the value from code?
If so, make sure that the shared prefs storage is the same in both cases:
Option 1:
- In your code snippet, call
PreferenceManager.getDefaultSharedPreferences() rather than
getSharedPreferences("settings"), to get the same shared prefs storage as
used by the preference activity.
Option 2, opposite of option 1:
- In your PreferenceActivity subclass, call
getPreferenceManager().setSharedPreferencesName("settings") telling the
prefs activity to use the same shared prefs storage as used in your snippet.
-- Kostya
2011/11/16 Larry/MavrickProductions <[email protected]>
> I have a need to store a single value - the year - so that it can be
> called from any activity in my app. Shared Preferences seems to be the
> easiest but I've got myself coded into a wall and could use a spot of
> advice.
>
> I think the problem is in my XML, but I'm not sure. Here's the
> pertinent snip of the java. It should be calling the "setyear"
> variable from the settings.xml file:
>
> SharedPreferences myPrefs = getSharedPreferences("settings",
> MODE_PRIVATE);
> final SharedPreferences.Editor prefsEditor =
> myPrefs.edit();
>
> int checkme = myPrefs.getInt("setyear", 0);
> if (checkme < 2011)
> {
> prefsEditor.putInt("setyear", 2011);
> prefsEditor.commit();
> }
>
> and the XML:
>
> <?xml version="1.0" encoding="utf-8"?>
> <PreferenceScreen
> xmlns:android="http://schemas.android.com/apk/res/android"
> >
> <EditTextPreference>
> android:key="setyear"
> android:defaultValue="2011"
> </EditTextPreference>
> <EditTextPreference>
> android:key="currlist"
> android:defaultValue="99"
> </EditTextPreference>
> </PreferenceScreen>
>
> Suggestions? Did I totally hose myself?
>
> Best, -Larry
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en