Here is what I encountered and what I did to fix it.
I was triggering an alarm from an Activity and in Broadcast-Receiver of that alarm I was updating Shared-Preferences which were read every time the app was launched.
After the alarm was triggered, whenever the app was launched it would get old values which were set from that activity only. No changes from Broadcast-Receiver were reflected.
The trick here is to set Shared-Preferences as MODE_MULTI_PROCESS
Generally we use MODE_PRIVATE, but do as follows:
SharedPreferences prefs = this.getSharedPreferences("Preferences", MODE_MULTI_PROCESS);
Note: After changing mode in code, it is advised to clear data of app to avoid issues while debugging.
EDIT:
MODE_MULTI_PROCESS need min API 11
Before API 11 the workaround which I can think of is creating a database with 2 columns KEY & VALUE.
This can be accessed from other modules.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…