Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

android - Difference between getApplicationContext and classname.this

When I'm using list view and I have a custom Base Adapter class, I get different text color in list view when base adapter is instantiated by getApplicationContext and classname.this. By getApplicationContext I get white text color but classname.this is black. Can anyone explain it for me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Most of the times it is better to use activity context.

Check the answer provided by commonsware. Has a detail explanation on the topic.

When to call activity context OR application context?

Quote form the above link

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

  1. 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.

  2. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...