Explain reasons why not to use getApplicationContext()?

Technology CommunityCategory: AndroidExplain reasons why not to use getApplicationContext()?
VietMX Staff asked 3 years ago

Here are reasons why not to use getApplicationContext() wherever you go:

  • It’s not a complete Context, supporting everything that Activity does. Various things you will try to do with this Context will fail, mostly related to the GUI.
  • It can create memory leaks, if the Context from getApplicationContext() holds onto something created by your calls on it that you don’t clean up. With an Activity, if it holds onto something, once the Activity gets garbage collected, everything else flushes out too. The Application object remains for the lifetime of your process.

See this table as a guidance for when to use the different types of Context: