How to access "Activity.this" in Kotlin?
Try this label instead
this@YourActivityName
You can get a reference to your MainActivity
object in Kotlin by using a qualified this
. e.g.:
class MyActivity : MainActivity() {
val builder = MaterialDialog.Builder(this@MyActivity)
}
Answer is: this@ActivityName
For example: You should use it if you would like to define "Context" in MainActivity.kt
var mContext:Context = this@MainActivity
Why? Because in Kotlin language @ has mean "of" such as:
val a = this@A // A's this
If you want to learn more information, you can look Kotlin Language website: This Expression in Kotlin
If you are calling Activity.this from an inner class, you have to put inner before the class
class MyActivity : MainActivity() {
// Call from class itself
val builder = MaterialDialog.Builder(this@MyActivity)
inner class Inner {
this@MyActivity // Call from the inner class
}
}