1

I'm wondering what the this keyword refers to in the below code (the code block is to request permission to access user location).

class RequiresLocation : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_requires_location)

        turnOnLocationButton.setOnClickListener {
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED){
                ...
            }
        }
    }
}

I checked the Android docs for checkSelfPermission() and it has this:

int checkSelfPermission (Context context, 
                String permission)

What does the context here specifically refer to? Is it the application as a whole not the activity?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • There you go. You can check the method definition for the Parameters being used . https://developer.android.com/reference/android/support/v4/content/ContextCompat#summary – Rohit Singh Jan 18 '19 at 05:58

4 Answers4

0

Context is Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Different methods by which you can get context

  1. getApplicationContext()
  2. getContext()
  3. getBaseContext()
  4. this (when in the activity class)

this -> refers to the context of the current activity.

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

I'm wondering what the this keyword refers to in the below code

In your code snippet

ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED

the keyword this refers to the current Activity instance.

For those among us who are used to writing Java code: in this case Kotlin is different from Java.

In Java, you would have to write RequiresLocation.this once you're "inside" the scope of a View.OnClickListener.

In Kotlin, simply this will do. But if you are working with Android Studio or IntelliJ Idea and continue typing by entering a @ right after the this, then the code completion will offer you this@RequiresLocation, so you can be sure that it is indeed the correct this.

What does the Context parameter in checkSelfPermission() refer to?

You can pass in any Context - an Activity, the Application, but also some type of Service (note that Application and Service both extend from ContextWrapper which according to the docs has seven direct and more than 40 indirect subclasses, one of them is Activity. All of them are valid arguments to checkSelfPermission().)

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • `RequiresLocation.this` would be `this@RequiresLocation`. – Martin Zeitler Jan 18 '19 at 00:50
  • @Martin Zeitler - if I was coding in Java and wanted to refer to the surrounding Activity, it would have to be "RequiresLocation.this". That's what I was trying to say. I'll edit my post to avoid misunderstandings – Bö macht Blau Jan 18 '19 at 05:14
  • only noticed, that there also is an equivalent qualified syntax in `Kotlin`... which refers to `it`, for the scope itself. – Martin Zeitler Jan 18 '19 at 05:18
  • @MartinZeitler - yes, `it` seems to refer to the "view" parameter in the OnClickListener's `onClick(view:View)` ( I'm still very much a Kotlin beginner, but Android Studio's code completion offers me lots of View methods after `it.`) – Bö macht Blau Jan 18 '19 at 17:58
0

It refers to the current instance of the RequiresLocation class.

Fully qualified would read more clearly like: RequiresLocation.this

So, as you noticed the signature of checkSelfPermission requires a Context, and "this" (the instance of RequiresLocation) can be passed as such context parameter because all Activities derive from Context. Take into account that since RequiresLocation derives from AppCompatActivity, such class is also a Context.

PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
0

Context refers to current activity state. We use context for getting information of current activity state. You can also refer the below link for getting detail information about Context. https://blog.mindorks.com/understanding-context-in-android-application-330913e32514

Android Geek
  • 8,956
  • 2
  • 21
  • 35