You only use getApplicationContext() when you know you need a Context for something that may live longer than any other likely Context you have at your disposal. Scenarios include:
- Use
getApplicationContext()if you need something tied to aContextthat itself will have global scope. I usegetApplicationContext(), for example, inWakefulIntentService, for the staticWakeLockto be used for the service. Since thatWakeLockis static, and I need aContextto get atPowerManagerto create it, it is safest to usegetApplicationContext(). - Use
getApplicationContext()when you bind to aServicefrom anActivity, if you wish to pass theServiceConnection(i.e., the handle to the binding) betweenActivityinstances viaonRetainNonConfigurationInstance(). Android internally tracks bindings via theseServiceConnectionsand holds references to theContextsthat create the bindings. If you bind from theActivity, then the newActivityinstance will have a reference to theServiceConnectionwhich has an implicit reference to the oldActivity, and the oldActivitycannot be garbage collected. - Some developers use custom subclasses of
Applicationfor their own global data, which they retrieve viagetApplicationContext(). That’s certainly possible.