0

I have a class called 'DataReference' which has a number of static variables which are accessed by various classes throughout program execution.

I have one variable 'public static int particlePercent = 100;' which can only be altered from the main menu activity's 'SettingView' activity.

However, when exiting the main menu activity's 'GameView' activity, the 'particlePercent' variable is always reset to 100, even though the value may have been edited from 'SettingView'.

I expect the variable to remain the same as it is static and not edited anywhere but the 'SettingView' activity.

Please let me know if you have any idea what might be going on, or if I need to provide further clarification on the situation.

Thanks.

Leif
  • 107
  • 1
  • 11
  • I would like to see your code for both accessing and setting the variable. Another option is to debug the code in question. I use IntelliJ for Android development and it has a great debugger. – Jesse Webb Jul 08 '11 at 19:01

4 Answers4

3

As you can see in the diagram in the Android documentation, any process whose application is paused, stopped or destroyed, it may be killed: diagram

When it gets killed, all classes are dropped from memory and thus all static variables are lost, which can happen as soon as you change the current activity.

To resolve this, such variables have to be saved somehow, for example in the onSaveInstanceState method of Activity.

Community
  • 1
  • 1
cfstras
  • 1,613
  • 15
  • 21
2

I have run into this issue quite a few times, though I am not sure if the cause has been the same. Android resets variables when its low on memory. This thread might help you more - Android : Static variable null on low memory

Community
  • 1
  • 1
Suchi
  • 9,989
  • 23
  • 68
  • 112
  • I will look into this, but I am sure that my programs use of memory is negligible. Thanks for the suggestion! – Leif Jul 11 '11 at 05:18
0

Is it being set on a separate thread? If multiple threads are accessing this value and it is being changed on a different thread, there is no guarantee that other threads will ever see the changes without synchronized access.

Robin
  • 24,062
  • 5
  • 49
  • 58
  • Yes. One activity call 'SettingView' is the only thing that changes the variable. Everything else only accesses the value. However, the variable is only reset to its original value after the main game thread has run? So, I'm not sure how its happening. But I will look into this possibility and let you know if I am able to solve this bugger – Leif Jul 11 '11 at 05:24
0

Static variables belong to a class, not an instance of the class.

Where is the value set initially?

You've got a few options to see how it's getting reset. If you want to see what's re-setting it, or if it's ever getting set.

  1. Make the variable static, but private, and write a public static getter and setter. Then set break points in the setter and just debug. When it's being set (if it's being set!), you can look at the stack to see why it's being set, and to what.
  2. If that's not your cup of tea, you can always throw an exception and immediately catch it, and print the stack trace. This will do the same thing for you - show you the call stack to you can see what code is re-setting it.
Travis
  • 3,737
  • 1
  • 24
  • 24
  • The variable in question is initialized to 100, which made me think initially that the class was somehow being initialized and thus reset even though it just contains static vars and is never instantiated. I will try your suggestion to see how the variable is being accessed. – Leif Jul 11 '11 at 05:21