So I guess I’m new to Android and confused about how android contextss work and what is included in a context and what not. I was wondering if I imported a java file with a class that implements the View.OnClickListener in an activity does that count as part of the context or no? Also if I have a class named NewOnClick that implements View.OnClickListener can I use android:OnClick=”NewOnClick” or does that ONLY work with methods. The reason I ask is that I want to write my handlers that will be included in almost activity.
Also is there any way that someone can explain what a context exactly does a lot of these other ones don’t really explain what it is for or does just says that its passed to other widgets and manages resources, which really isn’t very descriptive at all in my opinion, if there is anyway that you can explain it to me in a way that someone who has worked with GTK (gtkmm) or wxWidgets makes sense.
There is a simple way of looking at it, which is that Context is the base class for applications, Activities, services etc. Therefore, everything in an Activity inherits from Context. The Context in Android is the environment in which your currently executing code exists, meaning everything available to it and everything influencing it (at least, that you have access to).
For example, within an Activity, “this” refers to the current Activity and, since this inherits from Context, accesses everything within the current context plus those things defined in the Activity, e.g. your local fields, additional methods etc.
Take a look at the indirect subclasses here:
http://developer.android.com/reference/android/content/Context.html
NB. Never expose a context outside of the life cycle of the derived class. e.g. do not let any object with a life cycle greater than it’s container hang on to the reference.
http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
Hope this helps, rather than confuses.