Difference in context this and getContext()
Solution 1
In general there are two type of classes. Ones that extend ContextWrapper
class (Activity
, Service
, Application
) and those that do not extend it (like View
).
If class extends
ContextWrapper
then you can usethis
asContext
. Such classes normally do not havegetContext()
method.Those classes that do not extend
ContextWrapper
but still save and useContext
normally exposegetContext()
function. And you cannot usethis
asContext
in such cases.
And these two cases are mutually exclusive. At least I don't recall classes that extend ContextWrapper
and have getContext
at the same time.
Solution 2
getContext()
is not defined in an Activity. It's used in a View
(or View
subclass) to get a reference to the enclosing context (an Activity).
Solution 3
There is no difference. When you are in an Activity, getContext() will return this. This is because an Activity is a context!
Comments
-
Lukap almost 4 years
What is difference between
this
andgetContext()
, when I saythis
I meanthis
within anActivity
.