-3

Why we really need to pass context in some elements and what is the difference between getApplicationContext(), getBaseContext(), MainActivity.this ?

Hisham Muneer
  • 8,558
  • 10
  • 54
  • 79
  • 1
    There are many questions here that have the answer you are looking for. Have a look [here](http://stackoverflow.com/questions/3572463/what-is-context-in-android) for the exact same question. Also have a look at answers [here](http://stackoverflow.com/questions/4128589/difference-between-activity-context-and-application-context), [here](http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context) and [here](http://stackoverflow.com/questions/5018545/getapplication-vs-getapplicationcontext) – ebarrenechea Feb 21 '13 at 16:04

2 Answers2

1

This was asked and answered several times.

Please see this question: What is Context in Android? and the accepted answer: https://stackoverflow.com/a/3572553/1820695

For differences between various Context accessor methods (getApplicationContext(), getBaseContext(), this in Activity, etc.) see this question: Android - what's the difference between the various methods to get a Context?

Community
  • 1
  • 1
andr
  • 15,970
  • 10
  • 45
  • 59
  • 1
    Since this doesn't answer the question itself, this ought to be a comment. – Sam Feb 21 '13 at 16:06
  • @Sam you're most likely right given your rep - it just seemed like a place to list available answers, verified by myself. still, I'll keep that in mind in the future, thanks for pointing that out. – andr Feb 21 '13 at 16:12
  • 1
    "This was asked and answered several times." should be a comment and a close as duplicate. – njzk2 Feb 21 '13 at 16:13
0

If we go by the documentation

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

In Simple words its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application)

For the difference between getApplicationContext() and getBaseContext() please refer this

Community
  • 1
  • 1
silwar
  • 6,470
  • 3
  • 46
  • 66