android – What is context? A wider view

Question:

How many have been developing applications, I still do not understand what a context is. For example, take the good old Toast

 Toast.makeText(context, String, int);

The first parameter of the makeText static method of the Toast class is the context. But I can pass this (if the method call is within one block of code) or MainActivity.this (if the name of the activity class is MainActivity ), I can also pass getApplicationContext() .

  • So what's the difference?
  • Why can you pass this as the context? This is a class reference.
  • Are there any cases when it is necessary to pass exactly getApplicationContext ?
  • Why is context needed wherever there is work with the interface?
  • Why does any widget have a constructor that needs to be passed a context?
  • What is context anyway?

I asked the last question, because in the course of writing the rest, I realized that I don’t know anything about him 🙂

Answer:

the Context class contains all kinds of information about the resources of the system, as already mentioned in another answer. Specifically, in this question, we are interested in what it contains, among other things, and theme parameters (styles) for displaying View

Why can you pass this as the context? This is a class reference.

An activity is a descendant of the Context class and carries information about the context for this activity, so we can use a reference to this particular class as a context. With Fragment , for example, this no longer works – it does not inherit from Context

Are there cases when it is necessary to pass exactly getApplicationContext?

The theme (style) of the entire application and a specific activity may differ (a different style is specified for the activity in the manifest). Then a request for the application context and the activity context will return a different design View

Why is context needed wherever there is work with the interface?

Because it contains the style for the View

the answer to the rest of the questions is the same – the context contains information about how the View should look like. For example, the style of the Holo theme button and the AppCompat theme are very different, and the context contains this information.

Perhaps there may be some other differences in the application in the environment assigned to the entire application and a specific activity, then accessing the context of the application or activity will also matter, but I could not remember such differences (except for themes and styles).

UPD a few important notes on getApplicationContext() , not related to the application UI from this article

  1. application context should be used wherever the context needs to be passed outside the lifecycle of the transmitting component (into objects that will live longer than the activity that created/calls them, for example) to avoid keeping a reference to this component when using its own context and memory leaks.

  2. application context should be passed to external libraries for the same reasons as point 1

  3. the application context does not have information on the specifics of the GUI of a particular activity, if they differ from the settings of the entire application, in such cases you cannot use the application context when working with the GUI of this activity.

  4. application (class Application ) is a singleton and its context is also a singleton, this context can hold objects with a shorter life cycle and lead to memory leaks if you do not take care of their correct processing by GC

Scroll to Top