user11711721
user11711721

Reputation:

How to move from one activity to another i.e Java Class to Kotlin Class in an Android Java Project?

I am using the following ways but in all the cases in the first class which is Java Class, there is an error during run time. Here LoginActivity is the Java class and VerifyMobile Activity is the Kotlin class.

  1. Intent intent = new Intent(this, VerifyMobile.class);(Run Time Error)
  2. Intent intent = new Intent(LoginActivity.this, VerifyMobile.class);(Run Time Error)
  3. Intent intent = new Intent(this, VerifyMobile::class.java); (Compiler Error)
  4. Intent intent = new Intent(LoginActivity.this, VerifyMobile::class.java); (Compiler Error)

Looking for the solution.

  1. Make the changes in the first class which is Java class
  2. Make the changes in the second class which is Kotlin Class.

Upvotes: 0

Views: 521

Answers (2)

To init another activity you should create your intent with the current activity to the next one like this:

Java:

Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);

Kotlin:

val intent = Intent(this, AnotherActivity::class.java)
startActivity(intent)

or:

Java

startActivity(new Intent(this, AnotherActivity.class));

Kotlin:

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

Upvotes: 1

Eric
Eric

Reputation: 17536

In 3 and 4, it looks like Kotlin and Java syntax is being mixed together.

To create an intent in a Java file (.java), do this:

Intent intent = new Intent(context, VerifyMobile.class);

To create an intent in a Kotlin file (.kt), do this:

val intent = Intent(context, VerifyMobile::class.java)

It doesn't matter what language the Activity being navigated to is written in. What matters is the language of the file the code is being written in.

Upvotes: 1

Related Questions