Android: context object

When programming on Android, we always use a context object (perhaps the context keyword or the this ), but I really don’t understand what its purpose is.

For example, when we add a UI Component , for example TextView :

 TextView textView = new TextView(this); //this simple line make me headache setContentView(textView); 

The first time I think of a line: this keyword mean: this textView will be assigned to the current screen. But after that, I see that it is wrong thinking because the setContentView(textView) does what I think.

So who can explain to me what the purpose is when we declare a context object in the above example. (and other cases, if you want, tell me more: D)

thanks:)

+4
source share
3 answers

You will need the Context class when dynamically creating a view in action. For example, you can dynamically create a TextView from code. To do this, you create an instance of the TextView class. The constructor of the TextView class accepts a Context object, and since the Activity class is a subclass of Context , you can use the this to represent the Context object.

+7
source
An object

A Context provides access to application resources and other features. Each Activity is a Context , and each View needs a Context so that it can retrieve all the resources it needs (including things like system resources).

The second line tells the Activity use this particular View (a TextView ) as the top-level user interface element to display for this Activity . There is no conflict between this and using Activity as Context to build a TextView in the first place. These are different things.

+7
source

Why do we need context? The documentation says that for each view, a context is needed to access the necessary resources (for example, subject, lines, etc.).

But why in the constructor, and not through setContentView (View) ?

  • Since resources must be available at the time of creating the view (the designer will need some resources to completely initialize the view).

  • This allows you to flexibly use a context that is different from one of the current actions (imagine a view that uses some other string resources, rather than those from the current activity).

  • Android SDK developers seem to have chosen that the context should be set only once, and then remain unchanged throughout the life of the view.

Why is the context not automatically determined at the point of construction?

  • Since there is no static variable that will tell you the current global context of your application. The getApplicationContext () method is the closest to it, but it is not static, so you need an instance of the Activity object to call it.

  • The Java language provides the ability to view the call stack and determine whether the View created in the Context class. But what if there are a lot of them? Or what if they are not? This method is very expensive and error prone. Therefore, the developers of the API decided that the context should be provided manually.

+3
source

Source: https://habr.com/ru/post/1399628/


All Articles