Question

What is the Life cycle of static variables in Android?

I found that sometimes some static variables bound to activities happened to be uninitialized even though they've previously been initialized! I thought that when a static variable is initialized it stays so for the entire life of the application, but this doesn't seem to be the case.

Was it helpful?

Solution

Android may kill you app if it runs out of memory and all it's activities are in background (not visible). But you activity remains in history and user may activate it again. In this case system will recreate your app and restore it's state. To deal with this case you should store state of your app and activities in persistant memory (files, database, etc). More info you may find here http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState

OTHER TIPS

It depends were you define it, for example :

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

So whatever value you have set in Activity A will not persist. The same goes when an activity is destroyed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top