2

When an app have been put into the background and later resumes to an activity, is it possible that static class variables set in another Activity could have been reset by the garbage collector and have got the value set to zero?

I use a couple of public static int variables in my main Activity and use them as global variables in various other Activities. I have received a crash report in the developer console from Android Market where the only explanation I can find is that the app resumes to an Activity which uses the value of a public static int variable in another class, but the value has (mysteriously?) become zero. I know that it was set to something else when the app first started. Is this at all possible?

If my suspicion is correct, what is the recommended way to preserve the values of the global variables when an app is put in to background? Save them in SharedPreferences in OnPause() or use onSaveInstanceState or something else?

Anders
  • 109
  • 1
  • 8

3 Answers3

4

When an app have been put into the background and later resumes to an activity, is it possible that static class variables set in another Activity could have been reset by the garbage collector and have got the value set to zero?

It would not be "reset by the garbage collector". However, the process may have been terminated by Android, to free up memory for other applications.

what is the recommended way to preserve the values of the global variables when an app is put in to background?

Static data members should only be a cache. Data you want to retain regardless of what happens (e.g., process being terminated) needs to go in some persistent store, such as a flat file, SharedPreferences, or database. onPause() is a likely time to arrange to persist that data, as any time after that, your process could be terminated without notice.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I know that the process can be terminated by the system. That is not the problem. The problem is when the app resumes to an Activity which is not main activity. – Anders Jan 28 '12 at 16:36
  • @Anders: If the user brings up your application through recent tasks, control will return to whatever activity the user was last in. – CommonsWare Jan 28 '12 at 16:45
  • 1
    From the stack trace, I do only see system java files, not one of the files is from my source. This makes me think that the crash happens when the app resumes from background. It appears to crash when a value is set on a spinner which is not present in my main Activity. The size of the ArrayList used in the spinner was created in onCreate in a non-main Activity and uses the size of it from a global variable in my main Activity. That value appears to have been zero. – Anders Jan 28 '12 at 16:59
  • @Anders: Then the value either was set to zero by your code, or your process had been terminated and restarted. – CommonsWare Jan 28 '12 at 17:04
  • If it was terminated and restarted it should not go directly to onCreate() in a non-main Activity, would it? This is how I interpret what happens because the stack trace does not mention any of my source files but gives an exception on a spinner which is only present in a non-main Activity. Do you think my interpretation is correct? – Anders Jan 28 '12 at 17:31
  • @Anders: If the user brings up your application through recent tasks, control will return to whatever activity the user was last in. – CommonsWare Jan 28 '12 at 18:16
  • By "return to", do you mean that the Android system calls onCreate() for that Activity when this happens? And the original question is: could a global variable (public static int class variable) in another Activity have been lost (set to zero) in this situation? – Anders Jan 28 '12 at 19:11
  • @Anders: "By "return to", do you mean that the Android system calls onCreate() for that Activity when this happens?" -- if the process was terminated, yes. "could a global variable (public static int class variable) in another Activity have been lost (set to zero) in this situation?" -- if the process was terminated, yes. – CommonsWare Jan 28 '12 at 19:22
  • Thank you! That answered my question and this must be what has happened. I will create a UpdateGlobalVariables() method and call this in onCreate() and onResume() in all the Activities which uses these variables. I will save and restore them using SharedPreferences. – Anders Jan 28 '12 at 19:42
  • @Anders I too faced the same problem when the app goes in background and later resumes the static fields lose their values – Raj Jun 29 '13 at 17:57
1

Be careful with static variables. Follow the link for an explanation concerning Singleton (which also use a static variable to maintain state): https://stackoverflow.com/a/9004638/1127492

Community
  • 1
  • 1
Stefan
  • 4,645
  • 1
  • 19
  • 35
0

You can save this values on SQLite on method onDestroy in activity or another method with response to exit.

Namor Alves
  • 143
  • 1
  • 12