android - Static SharedPref gives uninitialized value in Broadcast Receiver -


i've kept static shared preference access values multiple activities.

now, i've set alarm go off @ time. in broadcast receiver, i'm trying access shared pref variable.

it has been initialized in activity , returns proper value there.

but in broadcast receiver doesn't give actual value. gives uninitialized value.

since static shouldn't value remain same?

here shared preference class.

package com.footballalarm.invincibles;   import android.content.context; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.util.log;  public class sessionmanagement { // shared preferences public static sharedpreferences pref;  // editor shared preferences public static editor editor;  // context context _context;  // shared pref mode int private_mode = 0;  // shared pref file name private static final string pref_name = "invincibles_alarm";  // shared preferences key public static final string pref_match = "match";  // constructor public sessionmanagement(context context){     this._context = context;     pref = _context.getsharedpreferences(getprefname(), private_mode);      editor = pref.edit();      editor.commit();      log.e("pref match value-constructor pref", getvaluematch()); }  public static void fillvalues(string match){ try{// storing login value true     editor.putstring(pref_match, match);      // commit changes     editor.commit();   log.e("pref match value-in fill values", getvaluematch()); } catch(exception e) {log.e("fillvalues", e.tostring());}  }     public static string getvaluematch(){            return pref.getstring(pref_match, "loading match");  } public static string getprefname() {     return pref_name; }  } 

i tried log output in other activities , returns properly.

when run app , close before alarm takes place, program crashes null-pointer exception since broadcast receiver cannot access shared pref.

i have tried solution - sharedpreferences in broadcastreceiver seems not update? i'm using name in manifest recievers.

this happens if close app in ics via minimize menu.

check link:

static variable loses value

maybe static variables loosing value in case .

static variables can loose value in following cases:

1) class unloaded.

2) jvm shuts down.

3) process dies.

instead of using static variables , functions , try using public class instead.

hope helps

edit 1:

example code of using public class preferences instead of static methods

public class preferenceforapp {     context context;     sharedpreferences prefs;      public preferenceforapp(context context) {         this.context = context;         prefs = context.getsharedpreferences(allconstants.prefs_name, 0);     }        public boolean getisdevicevalidated() {          return prefs.getboolean("validated", false);     }      public void setisdevicevalidated(boolean value) {          sharedpreferences.editor editor = prefs.edit();         editor.putboolean("validated", value);         editor.commit();     }   } 

in code add:

preferenceforapp mypref = new preferenceforapp(contxt); mypref.getisdevicevalidated(); 

useful related links:

android static object lifecycle

why static variables considered evil?

android : static variable null on low memory

edit 2

test when static variable loosing value :

you can test few lines of code:

  • print uninitialized static in oncreate of activity -> should print null

  • initialize static. print -> value non null

  • hit button , go home screen. note: home screen activity.

  • launch activity again -> static variable non-null

  • kill application process ddms(stop button in devices window).

  • restart activity -> static have null value.

i referred link android static object lifecycle


Comments