1

I have noticed, that when android OS closes activities which are not in the top of activity stack, some of my static variables become null. I'm absolutely sure, the variables pointed to objects before and that I did not change their value by myself.

After activity recreation I get nullPointerException cause one of my static variable (which is initialized in Application's subclass onCreate and is supposed to be not null at any time of process lifecycle) is null.

Since its not me who nulls the variable, I suppose it is android OS which closes background activities and nulls static variables due to lack of memory. Is it possible?

Eugene Chumak
  • 3,272
  • 7
  • 34
  • 52

2 Answers2

2

Android does not close activities which are not on top of the activity stack. If your application goes to the background and Android decides that it wants to reclaim the memory it just kills the process that hosts your activities. When the user returns to the application, Android creates a new process and recreates the activity that was on the top of the activity stack.

In most probability, that's what you are seeing. Obviously if your process is killed and recreated, your static variables will be null.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • where should I save this static variable.. Shared preferences ? – Vishal Khakhkhar Jun 11 '13 at 14:36
  • It depends. If these are simple variables then you can save them in shared preferences. If they are more complex you can either serialize them to a file or store them in an SQLite database. The other option is to save them to a Bundle in `onSavedInstanceState()` and when your activities are recreated, you can check if the static variables are null and if so, you can restore them from the saved instance state. – David Wasser Jun 11 '13 at 15:18
  • Other options. Lazy load them. Be careful with the threads (ie synchronize the method to "getInstance"), or put them in a custom Application object (or load them there). You don't know what Activity will be started, but onCreate in the Application will be called. http://developer.android.com/reference/android/app/Application.html#onCreate() Don't do I/O here, btw. – Kevin Galligan Jul 04 '13 at 05:41
  • Missed that part about the Application onCreate. Just read it. Would like to see the code. That *does* get called. Make sure you're not doing things in a background thread and calling the variable before its ready. If it turns out that onCreate isn't called, it would shatter my Android worldview as it currently stands. – Kevin Galligan Jul 04 '13 at 05:43
  • @KevinGalligan When Android creates a new process, it also instantiates the `Application` object and calls `onCreate()` on that. – David Wasser Jul 04 '13 at 07:55
1

They are only being nulled if the underlying VM/Thread that the activity was running in was killed. then it is like you are completely restarting the application. Don't rely on static variables, if you need to keep something around, store it in a DB or a Preference.

Kaediil
  • 5,465
  • 2
  • 21
  • 20