4

As you may see that

this@MainActivity or MainActivity@this

is valid while starting activity in kotlin. I tried to find out answer but I did not found anything. Can anyone know about exact difference of it and which one valid ? Why it allowed both ?

Community
  • 1
  • 1
Smeet
  • 3,787
  • 1
  • 32
  • 44

2 Answers2

7

According to Kotlin's Grammar documentation,

MainActivity@this is just a label for this with the name "MainActivity". You can set whatever label you want.

this@MainActivity is an actual reference of this from MainActivity

Hope I make it clear.

Ye Min Htut
  • 2,834
  • 13
  • 28
  • Thank you. Intent(this@MainActivity, SettingActivity::class.java) and Intent(MainActivity@this, SettingActivity::class.java) both are valid and working fine. Is there any reason or what statement is same as MainActivity.class and this within MainActivity. – Smeet Jan 10 '19 at 05:17
  • You might want to read about it here https://stackoverflow.com/questions/3434041/meaning-of-this-and-class-in-java?lq=1 – Ye Min Htut Jan 10 '19 at 07:40
4

this@MainActivity - it is a reference to current MainActivity instance.

MainActivity@this - it is definition of a name of the label, i.e. MainActivity.

More info about labels.

BigSt
  • 19,607
  • 4
  • 63
  • 81