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.