Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top