Skip to main content

Android: Adding a Second Activity

Goals

  • Create an additional activity
  • Create the ability to navigate from the Main Activity to the second Activity

 

Create a second activity

To create the second activity, right-click the app folder and select New > Activity > Empty Activity

  • Name the activity SecondActivity and click Save

This will:

  • Creates the SecondActivity file.
  • Create a  layout file activity_display_message.xml, which corresponds with the SecondActivity file.
  • Adds the required <activity> element in AndroidManifest.xml.

Add the Ability to navigate form one activity to another activity

In the file app > java > com.example.secondactivity > MainActivity

We are going to add some code to  MainActivity that starts the new second activity when a button widget is clicked

package com.example.secondactivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

 /** Create function to call when a user taps the button widget*/
        fun openSecondActivity(view: View) {

            // Do something in response to button
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
}

Intent represents an Android app’s intent to do something.

In this example Intent is an object that provides runtime binding between two activities.  

In the above example when the function openSecondActivity() is called we tell our app to start activity we've named SecondActivity

We want to call openSecondActivity() when we click the Button widget in MainActivity

We need to update our Main Activity layout to include a Button widget 

In app > res > layout > activity_main.xml  add the following code:

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="openSecondActivity"
        android:text="@string/next_activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This layout XML will create a Button widget, centered on the screen, with an OnClick attribute that calls the openSecondActivity function in  the file app > java > com.example.secondactivity > MainActivity

The button text is stored in our strings file app > res > values > strings.xml along with the app name

<resources>
    <string name="app_name">Two Activities</string>
    <string name="next_activity">Next Activity</string>
</resources>