-2

What is the lifespan of a static variable?
If I have a static ArrayList initialized in a singleton, should I expect the reference to be null only if the app is killed by the user? Or could it be null if some activities are killed in the background?

Kaushik
  • 6,150
  • 5
  • 39
  • 54
Jim
  • 3,845
  • 3
  • 22
  • 47

3 Answers3

1

static variable will not become null if an activity is killed.

AIMIN PAN
  • 1,563
  • 1
  • 9
  • 13
1

It is possible that a static variable become null without the app being killed. You can read more about it here in @CommonsWare post.

is it possible for Android VM to garbage collect static variables without killing the whole Android application?

Quoting:

What can happen is that the user is in your app, leaves the app via HOME (or a notification, or an incoming call, or the recent-tasks list, etc.), then later returns to your app via the recent-tasks list. If your process had been terminated during the time it was not in the foreground, your static data member will be null when your activity is started up from the recent-tasks list. Since the activity the user returns to may not necessarily be your launcher activity, your app may behave as though the static data member spontaneously turned null, even though it was because your process had been terminated and restarted.

Ricardo
  • 9,136
  • 3
  • 29
  • 35
0

Static variable remain alive until the whole app is killed or destroyed. If app is in pause mode, still static variable hold the value

Shaon
  • 2,496
  • 26
  • 27