irajDP
irajDP

Reputation: 31

How to start a Activity of Kotlin from Java android

Can I do this below prossess in android studio?

I have a Kotlin project that I create another activity with its Java class and I want to start activity of Kotlin with clicking botton in activity of java then it start Kotlin activity

Upvotes: 1

Views: 5363

Answers (3)

YZN
YZN

Reputation: 133

in java, it is

 startActivity(new Intent(currentActivity.this, nextActivity.class);

if you want to send data to the next activity in java

 Intent intent = new Intent(MainActivity.this, nextActivity.class);

 intent.putExtra("anyName", value);

 startActivity(intent);

for kotlin it is

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

if you want to send data to the next activity in kotlin

        val intent = Intent(this@MainActivity, SecondActivity::class.java)
        intent.putExtra("Name", name)
        intent.putExtra("Email", email)
        intent.putExtra("Phone", phone)
        startActivity(intent)

Upvotes: 0

Hardik Bambhania
Hardik Bambhania

Reputation: 1792

Yes you can start activity from java to Kotlin and vice versa.

from java

startActivity(new Intent(context,DestinationActivity.class))

from kotlin

startActivity(Intent(this, DestinationActivity::class.java))

Upvotes: 1

Chrisvin Jem
Chrisvin Jem

Reputation: 4060

Kotlin is interoperable with Java. Just start the activity using an Intent like you would normally do in Java.

Upvotes: 2

Related Questions