0

I have an Activity that needs to save a single primitive (not an object) that is used to alter the UI in onResume(). I store this primitive in a separate class with a static variable reference. I realize I could use SharedPreferences to store this variable, however, what I want to know is if using the static variable to hold this primitive could potentially create problems.

Thanks to everyone for their input.

Swifty McSwifterton
  • 2,637
  • 1
  • 30
  • 37
  • 1
    You could also extend Application. See this for an example: http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables – Jan S. Sep 20 '11 at 05:05

2 Answers2

1

static variables holds value till application is running, once application get destroy all static variables loses their references (non long term) while share preference holds the value even if application get destroy, so consistency is more in share preference

now its upto you whether you want the variable value consistent or not

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • True, but if the Activity is destroyed, it makes no difference for my particular app if that value is lost. Once the app starts over, having the static variable default to zero is fine. So, other than that, any issue? I don't see that there is, but I am hoping to find out if I am right. Thanks. – Swifty McSwifterton Sep 20 '11 at 05:02
  • ya, you are right, but it depends on your requirments we can say static(virtual) and share pref(persistent), thats all i know – Mohammed Azharuddin Shaikh Sep 20 '11 at 05:04
0

I DISAGREE with static variable loses their references. Even when activities that hold static variable values destroy, other activities can still access them. For example, a bitmap variable from another activity that is closed completely, can be used by another activity

coolcool1994
  • 3,704
  • 4
  • 39
  • 43