Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static variable null when returning to the app

In my app there's a static variable that's being set to null when I return to my app from the external browser. Seems like the app or some portion of it is killed if the external web page I'm launching is complex enough.

If the app were to be killed entirely and then relauched from the main activity that would be ok, but the relaunch is from the activity that started the browser - and it's not meant to set the app state so it's crashing when accessing the null static variable. This is a one-out-of-six device problem for me so I need some advice.

Is there a flag to set to prevent this behavior?

like image 780
jchristof Avatar asked Mar 02 '12 23:03

jchristof


People also ask

Can static variables be null?

Static variable can only be null if it's on it's class. If you call the static variable from different class the result is NOT null.

Why are static variables initialized to zero?

Yes, it's because it's in the standard; but really, it's because it's free. Static variables look just like global variables to the generated object code.

What happens when you call a static variable outside the package?

From outside the class, "static variables should be accessed by calling with class name." From the inside, the class qualification is inferred by the compiler.

Do static variables keep their value?

A static variable has a property to retain its value from it's previous scope. This means that it's value does not get re-initialized if the function in which it is declared gets called multiple times.


2 Answers

This is standard behavior in most mobile operating systems, definitely including Android. 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.

You should save your data somewhere more durable. You might find this article on general Data Storage to be useful. This question should be relevant too: Saving Android Activity state using Save Instance State

Note that this is in fact not a one-out-of-six device problem. This is a "problem" on all devices, it's just more apparent on one of your devices probably because it has less memory. If you run a very memory-intensive app on any of your other devices you should see the same behavior. Also there is no flag to prevent this. This is standard and expected.

like image 137
kabuko Avatar answered Oct 05 '22 18:10

kabuko


Usually this happens when the device goes to sleep mode.

This behavior of the device can be emulated by the following steps:

  1. Run the app and press the Home button
  2. In Android Studio in the lower left corner, select debugged the application and press the X button (terminate application) to the left of the application name. (Don't know how to Eclipse, but I think similarly)
  3. Click on the application icon on the device.

If the Task were activity, the app will open on the last activity and (most likely) will generate an error, because all static variables have been leaved.

like image 38
Vladimir Mamulov Avatar answered Oct 05 '22 19:10

Vladimir Mamulov