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). It can happen when-
-You force stop your app.
-Application crashes.
-You clear your app data.
-Switch off your Device(Shutdown DVM).
二:
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:
- print the uninitialized static in onCreate of your activity -> should print null
- initialize the static. print it -> value would be non null
- Hit the back button and go to home screen. Note: Home screen is another activity.
- Launch your activity again -> the static variable will be non-null
- Kill your application process from DDMS(stop button in the devices window).
- Restart your activity -> the static will have null value.
Hope that helps.
来自: http://stackoverflow.com/questions/1944369/android-static-object-lifecycle-application-act-crazy/1944564#1944564