2

Let's assume that I have an Android App with two Activities (Activity1 and Activity2). In Activity1 I declare a public static Boolean foo = true.

When I finish() Activity1 and move to Activity2, I am able to see that "foo" variable has value true

But when the System has low memory (e.g. because there are many apps running on the device) then, when I am on Activity2 I see that the value of "foo" variable is null.

How is this explained?

Fivos
  • 558
  • 8
  • 19
  • 2
    The answer to this question could be the solution for your problem. http://stackoverflow.com/questions/9541688/static-variable-null-when-returning-to-the-app – Kevin Sep 06 '15 at 09:32
  • What I don't understand is, when I finish() Activity1, why can I see the value of "foo" is true, and when the system kills it the value of "foo" is null – Fivos Sep 06 '15 at 09:43
  • Probably because `null` is the default value for your static `foo` variable, like pointed out in @Kishan Vaghela's answer. – Kevin Sep 06 '15 at 09:46

2 Answers2

4

It's important to note that the life of a static variable is tied to the process not the activity. Even if your activity is destroyed, the static variable will still be alive (which is why you see it's value set to true). It's only when the process is destroyed that the static variable will be freed properly.

This is also one of the reasons you shouldn't use static variables to hold references to activities, contexts, or views. Huge memory leaks waiting to happen.

For your particular scenario, this means that:

  • Act1 created & set the variable; You've moved from Act1 to Act2
  • The processes is killed in the BG
  • When the system attempts to restore you, you end up back at Act2
  • However, since the initialization of that variable happened in Act1, (which hasn't been initialized) the variable doesn't get set.
Colt McAnlis
  • 3,846
  • 3
  • 18
  • 19
  • Thank you @Colt McAnlis for your definite answer. In the senario that the process was destroyed, and I am in Activity2 of the app why the variable is null? My launcher activity is Activity1, so the variable "foo" should already have been initialized again to `true` before moving to Activity2. What am I missing? – Fivos Sep 06 '15 at 15:54
  • 1
    Edited my answer to be more specific to your scenario. – Colt McAnlis Sep 06 '15 at 17:30
0

If the process is killed then all static variables will be reinitialized to their default values.

So whatever value you have set in Activity1 will not persist

Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
  • I think that the process is not killed, because then my app would also be killed. Do you mean that Activity1 is killed? – Fivos Sep 06 '15 at 09:37
  • 1
    It is very likely that your process **is killed**. `Your app is in fact very often killed if some other application with higher priority (generally, if it's in the foreground it's higher priority) needs the resources. This is due to the nature of mobile devices having relatively limited resources.` --> http://stackoverflow.com/questions/9541688/static-variable-null-when-returning-to-the-app Please also read comments... – Kevin Sep 06 '15 at 09:41
  • I said above that i think the process is not killed because my app is in the foreground when this happens, so if the process was killed my app would close. – Fivos Sep 06 '15 at 09:49