Saturday, December 17, 2011

[android-developers] SharedPreferences variable question

I need to read a "year" variable to set a timer. I'm using shared
preferences to store the variable so it is available across the whole
app. At the completion of the timer, the year variable is advanced by
one and writes back to the shared preferences. I have chased and
checked and the year variable *IS* being advanced correctly.

Here's the problem: In the timer.class activity, the variable is *NOT*
being read. Here is the exact code that I'm using to read the
variable. setyear1 is a final int only so that I can show it in a
textview box to see what is being passed. In another activity, the
setyear variable is read as 2012 (correct); in this activity it
reports being 2036 (which is my dummyload in case the variable can't
be read).

SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences myPrefs = this.getSharedPreferences("settings",
MODE_PRIVATE);
final SharedPreferences.Editor prefsEditor = myPrefs.edit();

final int setyear1 = prefs.getInt("setyear", 2036);

What am I missing?

Best, -Larry

--
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
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment