I assume this is all I have to do to make sure the preferences are
saved:

SharedPreferences settings = MyActivity.this.getSharedPreferences
("MyAppName", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("prefname", "namevalue");
editor.commit();

Then to load them like this:

SharedPreferences  settings = MyActivity.this.getSharedPreferences
("MyAppName", 0);
String value = settings.getString("prefname", "defaultvalue");

On my device, I never have seen a preference that was saved
disappear.  But two users out in the field say that after saving the
information, about an hour later (or longer) the information
disappears.

The only way I see this happening as they describe is if the
preferences are somehow erased.  They say they are not uninstalling
the app when this happens.  I'm at a loss as to how to reproduce it,
until I can get more information from them.

I wrote a test app which just sets and gets preferences a number of
times, but it never fails to get a previously set value.  I don't know
how to force the app to terminate and relaunch many times to see if
that is the issue.  Perhaps by creating one which responds to Intents
to get/set the prefs?  Is that a way to simulate this?

On Nov 19, 2:50 pm, "Dianne Hackbod" <[EMAIL PROTECTED]> wrote:
> Are you sure you call commit() after done with your edits?  If you don't,
> then the changes won't be written to disk, and at whatever point your
> process is killed while in the background they will be lost.  That is the
> only way I can think of off-hand to explain what you describe.
>
>
>
>
>
> On Tue, Nov 18, 2008 at 9:09 PM, dar <[EMAIL PROTECTED]> wrote:
>
> > I'm having an issue where some users are seeing the SharedPreferences
> > disappearing after some time.  I have two separate Actvities, one
> > writing, and the other reading.  I assume these two activities are
> > both in the same process (they both run from the same app), but is
> > this causing the problem?  What's curious to me is that I can't
> > reproduce the problem myself on the G1.
>
> > On Nov 18, 9:36 pm, "Dianne Hackbod" <[EMAIL PROTECTED]> wrote:
> > > SharedPreferences should be completely thread safe.  Also, all of the
> > normal
> > > calls the system makes to your components happen in the same main thread
> > of
> > > the process, so except for special cases (as described in the app model
> > doc)
> > > you shouldn't be dealing with multithreading.
>
> > > Are all of these components running in the same process?  Right now
> > > SharedPreferences does not deal well with multiple components in separate
> > > processes trying to use the same preferences data.
>
> > > On Tue, Nov 18, 2008 at 6:24 PM, Eric B <[EMAIL PROTECTED]> wrote:
>
> > > > I have a PreferenceActivity that uses an xml file to automatically
> > > > populate and save the preferences.  Sometimes though, when that
> > > > activity is started, the preferences are blank.  However, if you go
> > > > back later, they will reappear.
>
> > > > This is causing a lot of headaches with my app.  I have a Service and
> > > > BroadcastReciever running in the background that are also accessing
> > > > and (sometimes) updating the preferences.  Are there synchronization
> > > > issues with shared preferences?  Maybe permission issues?
>
> > > > I'm really stuck here and would greatly appreciate any insight you can
> > > > offer.
>
> > > > Thanks,
> > > > Eric
>
> > > --
> > > Dianne Hackborn
> > > [EMAIL PROTECTED] Hide quoted text -
>
> > > - Show quoted text -
>
> --
> Dianne Hackborn
> [EMAIL PROTECTED] Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
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