Kotlin Android – Start Another Activity

When the Android Application we are developing has multiple Activities, we should be able to start another Activity from the current Activity. Meaning navigate between activities.

In this tutorial, we will learn how to start an Activity from current Activity, with an example Android Application.

Starting an activity could be triggered programmatically when an event occurs. Some of such events are

  • Clicking a Button or any other View
  • Downloading a file is completed
  • Clicking on an item in the Drawer

To start new (another) Android Activity from an Activity, follow these steps.

  1. In the current Activity, create an Intent with current Activity’s context and Next Activity Class passed as arguments.val intent = Intent(this, AnotherActivity::class.java)
  2. Call startActivity() method with intent passed as argument. Android OS does start the activity mentioned in the intent.startActivity(intent)

Example – Start Another Activity in Kotlin Android

  1. IDE Used : Android Studio
  2. Run : Android Application is run on an Android Device running Android 7.0.

In this example, we shall open new activity on button click from current activity. Consider that there are two activities in the Android Application : 1. MainActivity 2. AnotherActivity

AndroidManifest.xml containing these two activities is

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. This activity is displayed when you open this Android Application.

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>

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)
        }

    }
}

activity_another.kt

<?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>

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 this Android Application. Click on “START ANOTHER ACTIVITY” button. The application navigates to Another Activity. The flow of navigation would be as shown in the following video.

ADVERTISEMENT

Conclusion

In this Kotlin Android TutorialKotlin 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.