Android – When we start new activities to kotlin why we put .java in intent instead of .kt

According to kotlin doc, we use the following syntax to start a new activity

startActivity(Intent(this @ MainActivity,NextActivity :: class.java))

It’s kotlin, why do we add .java after class? Why not kt?

Because NextActivity :: class provides you with KClass< NextActivity>, and KClass has a name java methods/extended attributes, it provides you java.lang.Classfor a given class.

You can even check out the source -code for that java property.

According to the kotlin doc, we use the following syntax to start a new activity

startActivity(Intent(this @ MainActivity,NextActivity :: class.java))

It’s kotlin, why do we add .java after class? Why not kt?

Because NextActivity::class provides you with KClass, and KClass has a method/extension called java Attribute, it provides you java.lang.Classfor a given class.

You can even check out the source-code for that java property.

Leave a Comment

Your email address will not be published.