Lifetime of a static variable in Android

Can I still access the content of the variable?

Assuming that by "destroyed" you mean something like the user pressing BACK, yes.

Static data members live for the life of the process.

For example to always access a AsyncTask which I store to this variable? What I want is to be able to access to it also after an orientation change.

That is not an appropriate solution. Use a retained fragment, or use onRetainNonConfigurationInstance().


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

This is mainly because, when you restart the application, a new instance is created and the static variable will be reinitialized.


Static variables are associated with a class and they will live as long as the class is in the memory,and destroy when class gets unloaded (which very rarely happens).

In Android you have seen that when we close any application it does not close completely, It remains in the recent application stack, That you can see by holding in the home button(On Most Devices).

Android itself kicked out those recent app when the other app needs memory


Static variables are tied to the class itself. As long as the class is in memory, the variable will be kept.

Classes rarely get garbage collected as they live in what is called the Permanent Generation of memory space (you can find more about how generational GC works here https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html).

You can look at https://developer.android.com/topic/performance/memory-overview to get a better understanding of how memory is managed in Android, but unless your app is doing something very very unusual, the permanent generation is allocated all the memory it needs to hold all it's classes and will not be garbage collected.

Orientation changes will not clear a static variable, however if that's your goal using a static variable isn't very appropriate. You can keep instance state when orientation changes by using setRetainInstance or similar (see Android: how do I prevent class variables being cleared on orientation change for an answer)