How to jump from one activity to another activity in Kotlin

In this tutorial, we will learn how to jump from one activity to another activity in kotlin. This is the main activity we have made and there is three more activity we have created. We will use Intent().
It can be used with startActivity to launch an Activity. When you click on button or image it will jump to another activity. Below is a complete tutorial of how to jump from one activity to another activity in kotlin.
Here's a step-by-step guide on how to do it:
- Create the destination activity: Before you can jump to another activity, make sure you have the destination activity created. For example, if you want to navigate from "ActivityA" to "ActivityB," ensure that "ActivityB" is defined in your AndroidManifest.xml and the corresponding Kotlin file is created.
- Add a button (or any UI element) to trigger the navigation: In your source activity (e.g., "ActivityA"), add a button or any other UI element that the user can interact with to initiate the navigation to "ActivityB."
- Set up the click listener for the button: In the Kotlin file for "ActivityA," find the button (or UI element) by its ID and set up a click listener for it. Inside the click listener, you'll define the logic to create and start the intent to navigate to "ActivityB."
- Create and start the Intent: Inside the click listener, create an Intent object that specifies the context of the current activity ("ActivityA") and the target activity class ("ActivityB"). Then use the
startActivity()method to start the new activity.
Read More: Google Login And Registration For Android Using Firebase Authentication
MainActivity.java
import android.content.Intent
import android.media.Image
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
class MainActivity : AppCompatActivity() {
var photo: ImageView? = null;
var eduButton: Button? = null;
var wrkButton: Button? = null;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
photo = findViewById(R.id.profile_pic_small)
photo?.setOnClickListener({
var clickintent = Intent(this@MainActivity, Profilephoto::class.java)
startActivity(clickintent)
})
eduButton = findViewById(R.id.education_button)
eduButton?.setOnClickListener({
var clickedu = Intent(this@MainActivity, Education_Profile::class.java)
startActivity(clickedu)
})
wrkButton = findViewById(R.id.work_experience_button)
wrkButton?.setOnClickListener({
var clickwrkexp = Intent(this@MainActivity, WorkExperience::class.java)
startActivity(clickwrkexp)
})
}
}
In the above example, when the user clicks the "buttonNavigate" button in "ActivityA," it will create an Intent to navigate to "ActivityB" and start that activity using startActivity(). This will effectively switch from "ActivityA" to "ActivityB."
Remember to replace "ActivityA" and "ActivityB" with the actual names of your activities, and adjust the layout and button IDs accordingly based on your XML layout file.
If you have any confusion or need help then ask questions through the comment section we will help you out.
Frequently Asked Questions
How do I move from one activity to another in Kotlin?
Create an Intent with the current context and the destination class, then pass it to startActivity(): startActivity(Intent(this, ActivityB::class.java)). The destination activity must also be declared in AndroidManifest.xml.
How do I pass data between activities in Kotlin?
Attach extras to the Intent before starting it with putExtra("key", value), then read them in the destination activity from intent.getStringExtra("key") or the matching getter for the type you sent.
Why does my app crash with ActivityNotFoundException?
Almost always because the destination activity is missing from AndroidManifest.xml. Every activity you start must have its own <activity> entry there, even when the Kotlin class exists and compiles.
How do I go back to the previous activity?
Call finish() on the current activity to remove it from the back stack and return to the one beneath it. The system back gesture does the same thing, so you rarely need to start the previous activity again with a new Intent.
What is the difference between an explicit and implicit Intent?
An explicit Intent names the exact component to launch, which is what you use to move between activities inside your own app. An implicit Intent describes an action instead and lets Android choose an app that can handle it, such as sending an email or opening a URL.




![Google Map Tutorial in Android Studio [Step by Step]](/wp-content/uploads/2019/03/google-map-tutorial.png)
