4

My misunderstanding continues ...

Can anyone cite references for the proper use of get*Context()? I get conflicting recommendations about using getBaseContext(), getApplicationContext() and getContext() and my understanding is that using this is a convenience to get*Context(). I would like to study more specifically of what Dalvik is intending its object and access methods.

I had code reviews that changed my calls to getBaseContext() to getApplicationContext(), now I am seeing suggestions to use this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mobibob
  • 8,670
  • 20
  • 82
  • 131
  • Try to read this thread http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context – Shashank_Itmaster May 02 '11 at 04:07
  • Possible duplicate of [Difference between getContext() , getApplicationContext() , getBaseContext() and "this"](http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and) – rds Jun 16 '16 at 15:29

1 Answers1

1

http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html

Read this article. It describe why we should use getApplicationContext() rather than Activity's this

This is summary of the article:

In summary, to avoid context-related memory leaks, remember the following:

  • Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
  • Try using the context-application instead of a context-activity
Sean O'Toole
  • 4,304
  • 6
  • 35
  • 43
theWook
  • 843
  • 9
  • 22
  • 1
    don't forget they are all a little different. getContext() gets the CURRENT context, BaseContext() is usually the originating parent context. ApplicationContext is guaranteed to not have be fuddled with because it is associated with the application and not with any activity in particular. – Dr.J May 04 '11 at 01:54