0

I have a simple activity consisting of a main activity, a second activity and a preferenceactivity.

What I do:

  1. I run the app, mainactivity triggers oncreate
  2. Oncreate fetches sharedpreferences and prints the value to the console
  3. I go to the settings, change the value of the amount and press back.
  4. oncreate triggers again and logs the newly set value (good)
  5. The oncreate also triggers a repeating alarm. In my receiver class I also log the sharedpreferences.
  6. The problem: the value logged in my receiving class is still the old value!
  7. Next I completely close the app. The alarm keeps running (as it should) and now the receiver starts logging the new, correct value.

So, change preference value is immediately picked up in main activity Other classes requires the app to exit / restart to pickup the change.

I've been reading the google documentation about preferences, followed tutorials, ... been struggling with this for 3 evenings.

Advice would be appreciated.

EDIT: found what was the problem, had this in my manifest

<receiver android:name="Alarm" android:process=":remote" />

Because it was on another process the preference changes were not detected

Found it thanks to this post: https://stackoverflow.com/a/10770175/1323883

Community
  • 1
  • 1
Tim Reynaert
  • 145
  • 5
  • 19
  • same way? In the broadcast receiver you must use the context in onReceive(). Maybe you see the "default" value in your receiver. – greywolf82 Apr 16 '14 at 18:35
  • Please make sure that SharedPreferences is commit. can you please post your code – Venkat Apr 16 '14 at 18:38
  • I do, its indeed not exactly the same in my onReceiver, there it is SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); String amount = preferences.getString("notifications_new_message_amount", "1"); System.out.println(amount); – Tim Reynaert Apr 16 '14 at 18:48

0 Answers0