I do not fully understand the activity context vs the application context.
As I understand it so far, each is an instance of its class, which means that some programmers recommend you to use this.getApplicationContext() as often as possible in order to not "leak" out any memory. This is because the other this (getting the Activity instance context) points to an Activity that is being destroyed each time the user tilts the phone, or leave the app, etc., which the Garbage Collector (GC) apparently doesn’t catch and therefore the app uses too much memory.
But where would it be right to use this (getting the context of the current Activity instance) and the application context would be useless/wrong?
getApplicationContext()is almost always wrong. Ms. Hackborn (among others) have been very explicit that you only usegetApplicationContext()when you know why you are usinggetApplicationContext()and only when you need to usegetApplicationContext().To be blunt, “some programmers” use
getApplicationContext()(orgetBaseContext(), to a lesser extent) because their Java experience is limited. They implement an inner class (e.g., anOnClickListenerfor aButtonin anActivity) and need aContext. Rather than usingMyActivity.thisto get at the outer class’this, they usegetApplicationContext()orgetBaseContext()to get aContextobject.You only use
getApplicationContext()when you know you need aContextfor something that may live longer than any other likelyContextyou 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. I prefer static data members, if for no other reason than you can only have one customApplicationobject. I built one app using a customApplicationobject and found it to be painful. Ms. Hackborn also agrees with this position.Here are reasons why not to use
getApplicationContext()wherever you go:It’s not a complete
Context, supporting everything thatActivitydoes. Various things you will try to do with thisContextwill fail, mostly related to the GUI.It can create memory leaks, if the
ContextfromgetApplicationContext()holds onto something created by your calls on it that you don’t clean up. With anActivity, if it holds onto something, once theActivitygets garbage collected, everything else flushes out too. TheApplicationobject remains for the lifetime of your process.