Android static object lifecycle Android static object lifecycle android android

Android static object lifecycle


Lets start with a bit of background: What happens when you start an application?
The OS starts a process and assigns it a unique process id and allocates a process table.A process start an instance of DVM(Dalvik VM); Each application runs inside a DVM.
A DVM manages class loading unloading, instance lifecycle, GC etc.

Lifetime of a static variable: A static variable comes into existence when a class is loaded by the JVM and dies when the class is unloaded.

So if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens:
1. the class is unloaded
2. the JVM shuts down
3. the process dies

Note that the value of the static variable will persist when you switch to a different activity of another application and none of the above three happens. Should any of the above three happen the static will lose its value.

You can test this with a few lines of code:

  1. print the uninitialized static in onCreate of your activity -> should print null
  2. initialize the static. print it -> value would be non null
  3. Hit the back button and go to home screen. Note: Home screen is another activity.
  4. Launch your activity again -> the static variable will be non-null
  5. Kill your application process from DDMS(stop button in the devices window).
  6. Restart your activity -> the static will have null value.

Hope that helps.


Well, the Singleton pattern is also based on using static variables so actually you would be in the same position. While the static approach may work most of the times, it may happen that in some cases when memory is full and another activity takes the foreground before your application moves to its next screen, your activity's process could be killed and you lose the static values.However Android offers a few options of persisting values between states or transmitting them such as:

  • using an Intent, you could pass alongyour search criteria from activity toactivity (similar to a web httprequest)
  • using application preferences, youcould save the values and retrievethem in the activity that needs them
  • using the sqlite database you canpersist them in a table and retrievethem later
  • if you need to just save activitystate so that on restart, the fieldsget filled with their previouslyselected values, you can implementthe onSaveInstanceState() activitymethod - note that this is notrecommended for between activitiespersistance of states.

You can get some code examples of the usage of preferences, intents and the sqlite database by looking at the aegis-shield source code tree in google code or in other open source Android applications.


After some research, it turns out that using Application to store singletons is not that great of an idea, unless you are ready to recreate it:

Don't store data in the application object

so while the accepted answer is technically correct, it doesn't provide all information.

As the link above suggests, if you really want to stick with that model, you need to be ready to check for null and recreate the data, if possible.