How to jump from one activity to another activity in Kotlin
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.
Aditya Singh
With over seven years of experience, I help people understand technology through clear and insightful articles. I cover the latest in technology, mobile devices, PCs, how-tos, guides, news, and gadget reviews, always staying updated to provide accurate and reliable information.
Related Posts
7 Top Samsung Galaxy Ring Alternatives for 2025
Tired of waiting for the Samsung Galaxy Ring to hit the market? You’re not alone. The smart ring market already has many impressive alternatives available now, despite the buzz around Samsung’s upcoming smart ring. These devices pack advanced health tracking and contactless payment features that might surpass Samsung’s planned offerings. The current lineup of smart […]
What Is Quiet Mode on Instagram and How to Activate It
Ever wondered what Quiet Mode on Instagram is all about? This simple yet powerful Instagram feature helps you take a break from the constant buzz of notifications and focus on what truly matters. Whether you’re striving for better work-life balance, dedicating time to studying, or simply trying to disconnect from social media distractions, Quiet Mode […]
How to Make a Bed in Minecraft (Step-by-Step Guide)
A bed in Minecraft is very important. It lets you skip the night and set your spawn point, so if you die, you will return to your bed instead of the original world spawn. This guide will show you how to gather materials, craft a bed, and set your spawn point. We’ll also show you how to customize your bed, build bunk […]
10 Best MMORPG Games For Android
Not too long ago, MMORPG games and powerful gaming consoles were mostly exclusive to PCs. They required high-end graphics and systems to deliver their immersive gameplay. But times have changed. With the rise of gaming-oriented smartphones, you can now enjoy PC-like gaming experiences on your Android device. Thanks to these technological advancements and faster internet […]
Roblox: Fruit Battlegrounds codes (January 2025)
Fruit Battlegrounds codes are all about getting more gems and help you to shoot up your rank in this One Piece anime-inspired game. This Fruit Battlegrounds was made by Popo developer. It is an action-packed game where players battle it out using unique fruit-based abilities. With constant updates, new fruits, and exciting challenges, it’s a fruity frenzy you won’t […]
Roblox: Ultimate Football Codes (January 2025)
Want to get some extra items for Ultimate Football in Roblox? You’ve come to the right place! Here’s the latest list of codes to help you score touchdowns and look stylish on the field. These codes offer free rewards such as coins and cosmetics to enhance your gameplay. What are Ultimate Football Codes? Ultimate Football […]
Roblox: Da Hood Codes (January 2025)
Are you a fan of Roblox games, in this article we will look at the Roblox Da Hood Codes for December 2024 that will help you unlock exclusive items, improve your gameplay and dominate the streets of Da Hood. You can feel that the game is inspired by the Grand Theft Auto series and there […]