HomeTutorialsHow to jump from one activity to another activity in Kotlin

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:

  1. 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.
  2. 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.”
  3. 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.”
  4. 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.

Related Articles

Latest news

Unveiling the Darkest Elements in Hogwarts Legacy: Top 10 Elements

Hogwarts Legacy (Buy Now) is a jewel of a game for all the Harry Potter fanbase, inviting them to experience the magical universe of...

Dead Island 2 Cable Guy’s Van Keys location

As you embark on your journey in Dead Island 2, you'll stumble upon a mysterious vehicle known as the Cable Guy Van. This van...

iotty Smart Switch – Complete Review

The smart home industry is rapidly evolving with a plethora of innovative devices vying for consumers' attention. Among these, the iotty Smart Switch stands...

How To Fix Snapchat Support Code SS06

Every Snapchat user cherishes the joy of sharing moments, using filters, and connecting with friends. However, this joy can be interrupted by various glitches...

Can You Refund Fortnite Accounts?

Fortnite, the battle royale game developed by Epic Games, has taken the gaming world by storm since its inception. With its captivating gameplay, interactive...

Stay in the Loop

Get the weekly email from Android Hire that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.