So I have log messages that get displayed whenever onPause, onStop, and onDestroy get called.
I don't actually save my preferences in any of these methods, I save them when the values get changed by the user. By watching the logcat messages I have determined that my preferences get deleted (do not persist) only when the onDestroy() method is called but when onStop is called the preferences load perfectly fine the next time my activity starts. On Dec 18, 5:07 pm, Mark Murphy <[email protected]> wrote: > 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/commonsguyhttp://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

