What does the context of "this" mean?

I have watched many Android tutorials on the internet. In these tutorials, they use this context for context around the world. I know what the this keyword means in Java, but I can't make it equal, with the this in Android programming. For example, on AlertDialog.Builder , on developer.android.com, there is only one link in the context options, but I cannot find out what this means here.

+3
source share
6 answers

if you have an action, you can use this because:

  • this is the current instance of the class
  • The action inherits from the Context class.

so you can use your current activity as a context.

Look here for an Acitivty document.

and here to explain this

+12
source

If you know the variable "this" .. then u may also know that it contains a link for the current object .. so if any method requests a Context object, you can pass this variable only when it extends classes such as Context or Activity. Since Activity implements a context so restrained that it is a context

0
source

The context of the current class is called This in Android.

0
source

Here's another question where they explain Context . Because actions and services are extended by Context , this context is a reference to this action or service.

0
source

The this is just a reference - inside the class instance - for itself.

There are two common uses of "this" in Android. This does not apply to Android, but they are valid in Java in general.

  • this.getResources() talks about calling the getResources() method on the this instance. this part of it is usually not needed and is used implicitly.

  • Foo.this.getResources() can be called from an inner class. This may be convenient if the inner class also defines getResources() , but you want a version defined in another class.

0
source

this is a reference to the current object

You can use this to refer to any current instance and any instance of its superclass.

If your class extends Activity . This is a case of inheritance. your class is a subclass, and the Activity class is the parent. then you can use this keyword to get an instance of Activity beacause. Activity class is the super class of your class. This is implicit casting.

also the Activity Context class as a superclass. therefore, you can reference an instance of this class using this keyword.

thanks

0
source

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


All Articles