Reputation: 30284
When programming on Android, we use context object everywhere (maybe context
keyword or this
keyword), but I don't really understand what its purpose.
For example, when we add UI Component
such as TextView
:
TextView textView = new TextView(this); //this simple line make me headache
setContentView(textView);
The first time I think above line is : this
keyword mean: this textView will be assign to current screen. But after that, I see that is a wrong thinking because line setContentView(textView)
do what I think.
So, who can explain for me, what purpose when we declare context object
in above example. (and others case, if you please, tell me more :D)
thanks :)
Upvotes: 4
Views: 6337
Reputation:
You will need the Context
class is when creating a view dynamically in an activity. For example, you may want to dynamically create a TextView
from code. To do so, you instantiate the TextView
class. The constructor for the TextView
class takes a Context
object, and because the Activity
class is a subclass of Context
, you can use the this
keyword to represent the Context
object.
Upvotes: 7
Reputation: 1103
Why does it need a context? The documentation says that every view needs the context to access the right resources (e.g. the theme, strings etc.).
But why in the constructor and not through setContentView(View)?
Because the resources must be accessible while the view is being constructed (the constructor will need some resources to fully initialise the view).
This allows the flexibility of using a context that is different from the one of the current activity (imagine a view that uses some other string resources and not the ones from the current activity).
The designers of the Android SDK seem to have chosen that the context must be set only once and then stay the same throughout the lifetime of the view.
Why isn't the context determined automatically at construction point?
Because there exists no static variable that would tell you the current global context of your application. The method getApplicationContext() is the closest to this, but it's not static, so you need an instance of the Activity
object to call it.
The Java language provides an option to look through the call stack and find whether the View
has been constructed in a Context
class. But what if there are many? Or what if there are none? This method is very expensive and error prone. So the designers of the API decided that a context must be manually provided.
Upvotes: 3
Reputation: 234857
A Context
object provides access to the application's resources and other features. Each Activity
is a Context
and each View
needs a Context
so it can retrieve whatever resources it needs (including things like system-defined resources).
The second line tells the Activity
object to use that particular View
(a TextView
) as the top-level user interface element to display for that Activity
. There is no conflict between that and using the Activity
as a Context
for constructing the TextView
in the first place. They are different things.
Upvotes: 7