Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access "Activity.this" in Kotlin?

I have this piece of Java code:

MaterialDialog builder = new MaterialDialog.Builder(MainActivity.this)

I want to get the MainActivity object in Kotlin. The automatic conversion breaks at MainActivity.this.

like image 706
Rado Avatar asked Oct 09 '22 03:10

Rado


2 Answers

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)
}
like image 306
mfulton26 Avatar answered Oct 11 '22 06:10

mfulton26


Try this label instead

this@YourActivityName
like image 32
The Bala Avatar answered Oct 11 '22 06:10

The Bala