Reputation:
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.
Intent intent = new Intent(this, VerifyMobile.class);
(Run Time Error)Intent intent = new Intent(LoginActivity.this, VerifyMobile.class);
(Run Time Error)Intent intent = new Intent(this, VerifyMobile::class.java);
(Compiler Error)Intent intent = new Intent(LoginActivity.this, VerifyMobile::class.java);
(Compiler Error)Looking for the solution.
Upvotes: 0
Views: 521
Reputation: 165
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
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