If you want to ensure the data is saved, it must be done in onPause(). Under low memory situations, an activity can be killed before its onStop() is called.
On Sat, Dec 18, 2010 at 2: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]<android-developers%[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]<android-developers%[email protected]> > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- 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

