Start Another Activity in Kotlin Android
To open another Activity in a Kotlin Android application, create an explicit Intent that identifies the destination Activity and pass it to startActivity().
val intent = Intent(this, AnotherActivity::class.java)
startActivity(intent)
Here, this is the current Activity context and AnotherActivity::class.java identifies the Activity to open. Android places the new Activity on the task’s back stack. Pressing the system Back button normally returns the user to the previous Activity.
Starting another Activity may be triggered by events such as:
- Selecting a button or another view
- Selecting an item in a navigation drawer or menu
- Opening a detail screen for a selected list item
- Completing an operation that should lead to another screen
Steps to Open Another Activity with an Explicit Intent
- Create the destination Activity class and its layout or Compose content.
- Declare the destination Activity in
AndroidManifest.xml. Android Studio may add this declaration automatically when you create an Activity using a template. - Create an explicit
Intentin the current Activity. - Call
startActivity(intent)from the event that should open the destination screen.
Explicit Intent Syntax for Starting an Activity
val intent = Intent(currentContext, DestinationActivity::class.java)
startActivity(intent)
An explicit Intent is appropriate when the application knows the exact component it wants to open. An implicit Intent describes an action, such as opening a web page, and lets Android find an application capable of handling that action.
Open a New Activity on Button Click in Kotlin
The following example contains two Activities:
MainActivity, which contains the button and is launched when the application startsAnotherActivity, which opens when the user selects the button
The original example below was created with an older Android support-library setup. Its Activity-launching logic remains the same, but current projects normally use AndroidX, View Binding, or Jetpack Compose instead of Kotlin Android synthetics.
Declare Both Activities in AndroidManifest.xml
AndroidManifest.xml
...
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnotherActivity"></activity>
...
MainActivity is the launcher Activity, so it is displayed when the application opens. AnotherActivity is an internal destination without a launcher intent filter.
In a current Android project targeting Android 12 or later, an Activity containing an intent filter must explicitly specify android:exported. An Activity used only inside the application can normally be marked as not exported.
<application ...>
<activity
android:name=".AnotherActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Create the Main Activity Button Layout
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="This is first Activity"
android:textSize="25sp"
android:padding="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnStartAnotherActivity"
android:text="Start Another Activity"
android:textColor="#FFF"
android:padding="20sp"
android:background="#397bb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Call startActivity() from MainActivity
MainActivity.kt
package com.tutorialkart.anotheractivity
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnStartAnotherActivity.setOnClickListener {
val intent = Intent(this, AnotherActivity::class.java)
// start your next activity
startActivity(intent)
}
}
}
The click listener creates an explicit Intent and immediately starts AnotherActivity.
Create the Destination Activity Layout
activity_another.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AnotherActivity">
<TextView
android:text="This is another Activity"
android:textSize="25sp"
android:padding="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Implement AnotherActivity in Kotlin
AnotherActivity.kt
package com.tutorialkart.anotheractivity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class AnotherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_another)
}
}
Run the Android application and select the Start Another Activity button. Android opens AnotherActivity. Press Back to remove it from the back stack and return to MainActivity.
Start Another Activity with AndroidX and View Binding
Kotlin Android synthetics and the old android.support.v7 package shown in the original example are no longer used in current Android projects. With AndroidX and View Binding, the current Activity can be written as follows:
package com.tutorialkart.anotheractivity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.tutorialkart.anotheractivity.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnStartAnotherActivity.setOnClickListener {
val intent = Intent(this, AnotherActivity::class.java)
startActivity(intent)
}
}
}
The corresponding destination Activity uses the AndroidX version of AppCompatActivity:
package com.tutorialkart.anotheractivity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class AnotherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_another)
}
}
Pass Data to Another Activity with Intent Extras
Use Intent extras when the destination Activity needs a small value such as an identifier, title, or user-selected option. Use the same key when storing and retrieving the value.
MainActivity.kt
val intent = Intent(this, AnotherActivity::class.java).apply {
putExtra("EXTRA_MESSAGE", "Hello from MainActivity")
putExtra("EXTRA_ITEM_ID", 42)
}
startActivity(intent)
AnotherActivity.kt
val message = intent.getStringExtra("EXTRA_MESSAGE").orEmpty()
val itemId = intent.getIntExtra("EXTRA_ITEM_ID", -1)
Use a default value such as -1 when reading a primitive extra so the destination can detect that the expected value was not supplied. For larger or complex data, pass an identifier and load the data from a repository instead of placing the entire object in the Intent.
Start Another Activity from an Android Fragment
A Fragment is not a Context. Use requireContext() when creating the Intent, and call the Fragment’s startActivity() method.
binding.openButton.setOnClickListener {
val intent = Intent(requireContext(), AnotherActivity::class.java)
startActivity(intent)
}
Call this only while the Fragment is attached. An event handler connected to the Fragment’s active view is an appropriate place for it.
Start Another Activity from Jetpack Compose
Inside a composable, obtain the current context with LocalContext.current. Create and start the Intent from the button’s click handler.
import android.content.Intent
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
@Composable
fun OpenActivityButton() {
val context = LocalContext.current
Button(
onClick = {
val intent = Intent(context, AnotherActivity::class.java)
context.startActivity(intent)
}
) {
Text("Open another activity")
}
}
If all screens are composables in a single-Activity application, use Navigation Compose to move between composable destinations. Start a separate Activity when the destination is implemented as an Activity or belongs to a different application flow.
Open an Activity and Receive a Result
Use the Activity Result API when the second Activity must send a value back. This API replaces the older startActivityForResult() and onActivityResult() pattern.
private val editMessageLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val message = result.data
?.getStringExtra("RESULT_MESSAGE")
.orEmpty()
}
}
private fun openEditor() {
val intent = Intent(this, AnotherActivity::class.java)
editMessageLauncher.launch(intent)
}
The destination Activity can return the result before closing:
val resultIntent = Intent().apply {
putExtra("RESULT_MESSAGE", "Value from AnotherActivity")
}
setResult(Activity.RESULT_OK, resultIntent)
finish()
Explicit and Implicit Intents in Kotlin Android
| Intent type | Use | Kotlin example |
|---|---|---|
| Explicit Intent | Open a known Activity, usually within your application | Intent(this, AnotherActivity::class.java) |
| Implicit Intent | Request an action that another capable component may handle | Intent(Intent.ACTION_VIEW, uri) |
Use an explicit Intent for navigation between your own Activities. Use an implicit Intent for actions such as opening a web address, displaying a map location, or sharing text with another application.
Fix Common startActivity() Problems
ActivityNotFoundException When Opening Another Activity
Confirm that the destination Activity is declared in AndroidManifest.xml and that its package-qualified class name is correct. For an implicit Intent, verify that at least one installed application can handle the requested action.
Unresolved Reference for the Destination Activity
Check the destination class name, package declaration, and import. The Activity class must be available to the application module containing the launch code.
Wrong Context Passed to Intent
Use this or this@MainActivity inside an Activity. Use requireContext() inside an attached Fragment and LocalContext.current inside a composable.
Android 12 Manifest Exported Error
An Activity with an intent filter must declare android:exported="true" or android:exported="false". A launcher Activity must be exported so so the system launcher can open it.
Multiple Copies of the Same Activity
Every normal startActivity() call may add another Activity instance to the back stack. Review the Activity’s launch mode or appropriate Intent flags only when the application requires different back-stack behavior.
Kotlin Android Activity Navigation FAQs
How do I start another Activity in Kotlin?
Create an explicit Intent with the current context and destination class, then pass it to startActivity(): startActivity(Intent(this, AnotherActivity::class.java)).
How do I open a new Activity on a button click?
Set a click listener on the button and call startActivity() inside that listener. With View Binding, access the button through the generated binding property.
Must AnotherActivity be added to AndroidManifest.xml?
Yes. Every Activity must be declared in the application manifest. Android Studio usually adds the declaration when an Activity is created from an Activity template, but the manifest should still be checked.
How do I pass data from one Activity to another?
Add values to the Intent with putExtra(). In the destination Activity, read those values from its intent property using the same keys.
Should I use startActivityForResult() to get data back?
Use the Activity Result API in current Android code. Register a launcher with registerForActivityResult() and launch the Intent through that launcher.
Kotlin Activity Launch Review Checklist
- Confirm that the destination Activity is declared in
AndroidManifest.xml. - Verify that Activities with intent filters have the correct
android:exportedvalue. - Use an explicit Intent for navigation to a known Activity.
- Pass the correct context for Activity, Fragment, or Compose code.
- Check that Intent extra keys and data types match in both Activities.
- Use the Activity Result API when the destination must return data.
- Test forward navigation and system Back behavior on a device or emulator.
Summary of Starting Another Android Activity
In this Kotlin Android Tutorial – Kotlin Android Start Another Activity, we have learnt to start a new activity on button click. Infact the same code snippet could be used in any event handler just like button setOnClickListener.
The essential operation is to create an explicit Intent for the destination Activity and call startActivity(). Current projects can perform this operation from an AndroidX Activity, Fragment, or Jetpack Compose click handler. Intent extras can pass data forward, while the Activity Result API can return a result to the calling Activity.
TutorialKart.com