Generally, I recommend PreferenceManager.getDefaultSharedPreferences()
to get the SharedPreferences, as that will be the one that
PreferenceActivity uses. Otherwise, your code seems fine. However,
bear in mind that onDestroy() is not guaranteed to be called, so you
may be better served writing them in onStop(), anyway.

On Sat, Dec 18, 2010 at 5:01 PM, neuromit <[email protected]> wrote:
> I've been trying to save some state variables in the SharedPrefs, this
> seems to work fine when onPause, and onStop are called but not when
> OnDestroy() is called. When that happens the next time my activity
> loads the preference values have disappeared.
>
> Here is the code I use to save the variables:
>
> SharedPreferences settings = getSharedPreferences("PrivateVariables",
> MODE_PRIVATE);
> SharedPreferences.Editor prefEditor = settings.edit();
> prefEditor.putBoolean("playingState", playingState);
> prefEditor.putString("songInfo", songInfo);
> prefEditor.commit();
>
> and here is how I retrieve them:
>
> prefs = PreferenceManager.getDefaultSharedPreferences(context);
> playingState = prefs.getBoolean("playingState", true);
> songInfo = prefs.getString("songInfo", " No Value ");
>
> Is anything I'm doing obviously wrong?
>
> --
> 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
>



-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
Available!

-- 
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

Reply via email to