I've never tried java.util.Preferences on Android, but
android.content.SharedPreferences has been working great. The official
example uses Boolean, but it supports Long as well:
http://developer.android.com/intl/fr/guide/topics/data/data-storage.html#pref

On Sep 25, 6:10 am, leden <[email protected]> wrote:
> Hi all!
> My app needs to save its state before being killed, and since that
> state is really small (one Long), I've decided to use
> java.util.Preferences. I use it like the way I would be using in any
> Java application (and never had problems), though I am not sure this
> is the correct way here on Android platform:
> private static final Preferences prefs = Preferences.userNodeForPackage
> (WordChoice.class);
>
> I update preferences in onPause() method, to ensure that data is
> always persisted no matter how my app ends:
> Here is that fragment of code:
> protected void onPause() {
>         super.onPause();
>         prefs.putLong("currIndex", somePositiveValue);
>
> }
>
> Everything works perfectly on emulator, but when I install my app onto
> my g1 phone, the preferences doesn't work the way they are supposed to
> work - it seems that SOMETHING clears them every time my application
> is killed.
> Here is the example of output of my application:
> (FIRST STARTUP)
> no entry found for currIndex
> (STOPPED)
> (STARTUP)
> currIndex = 25
> (STOPPED)
> (STARTUP)
> currIndex = 83
> (KILLED i.e. from ASTRO or via terminal-emulator or killed by system)
> (STARTUP)
> no entry found for currIndex <-- what the ...??
>
> I really don't understand this behavior. I also tried flushing the
> preferences but doesn't help.
> Then I changed the code in onPause() method so that it first persists
> data then calls super.onPause() but then the even worse scenario
> occurs - preferences are cleared on random (about 50% to be cleared)
> no matter how my app ends.
>
> Please help me by giving, I have already spend a few hours struggling
> with this issue with no success.
> I would be grateful if someone posted an example of how to permanently
> store that a single "Long" and how retrieve it, so that it works when
> an application is killed.
--~--~---------~--~----~------------~-------~--~----~
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