Difference between getApplicationContext and classname.this

When I use the list view and I have my own base adapter class, I get a different text color in the list view when getApplicationContext and classname.this created by the base adapter. getApplicationContext I get text white, but classname.this black. Can anyone explain this to me?

+6
source share
2 answers

These are basically both instances of Context, but the difference is that the application instance is tied to the application life cycle and the Activity instance is tied to the Activity life cycle. Thus, they have access to various information about the application environment ...

see getApplicationContext

EDIT

When searching for an answer, it will help you Change the style of Android Holo Light depending on the selected context

+6
source

ActivityName.this refers to an activity context. getApplicationContext () refers to the application context.

In most cases, it is better to use an activity context.

Check out the answer provided by commonsware. Detailed explanation on this topic.

When to call an activity context or application context?

Quote from the link above

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

  • This is not a complete context that supports all actions. The various things you try to do with this Context will fail, mainly related to the GUI .

  • It can create memory leaks if the Context from getApplicationContext () rests on something created by your calls on it that you are not clearing. With Activity, if it rests on something, as soon as the Activity receives garbage collection, everything else is also dumped. The Application object remains during the lifetime of your process.

+6
source

All Articles