One slight difference is that the Garbage Collector
will first destroy static references bound in Activities
(or Services
) in case of intense memory shortage if it was in a situation of choosing between a static reference and a reference inside Application
class. This situation happens because when the Activity
(or Service
) is destroyed, it will leave the static variables without references (if they have no other reference except the one described above), so they can be collected by the GC. Even if the VM reinitialize the Activity
(or Service
), those static references will take the initial value losing any updates that may have been occurred. As a general rule of thumb, if you want to be sure that the static variables will be persistent:
- Do not reference them from
Activities
(or Services
) since there is a chance that they might be destroyed in case of memory shortage.
- Reference them from Activities or Services but handle the situation of them being destroyed manually (for example, with the
onSavedInsanceState
method of Android) just like will you do with non static references.
EDIT Here is an explanation of why is this happening:
Static references are bound to the class loader of the class
that first initialized them. This means that that if a static variable
inside any class has been initialized by an activity, when that
activity is destroyed also its class might be unloaded and so the
variable becomes uninitialized. While if the variable is initialized
by the application class, it’s life is the same as the application
process so we’re sure that it will never become uninitialized again.
That’s why I chose to initialize all the singletons in the
MyApplication class.
found in this link.