Lesson 11 – Launch a second “Activity” – example

So far all programs have had a single window (Activity)

It is very common for an application to have more than one window. To implement this in Android we must pose another two files one XML with the interface and another * .kt (Kotlin) with the logic (note that when we use Android Studio automatically when we create a project we create the XML file and the code in Kotlin of the first Activity)

Let’s see in this concept the steps we must take to create another Activity and how to activate it from the main Activity.


Make a program that shows in the main window a button that when pressed shows another window (Activity) showing a TextView with the name of the programmer of the application and a button to close the window or activity and return to the first Activity.

1 – First we create a new project that we will call Project010 and in the main window we create the following interface:

Lesson 11 - Launch a second

In other words, we want the “ABOUT” button to open another window (Activity) and show us the name of the programmer and a button to close the window.

2 – Now we have to create the second Activity. For this we right click on the Project window where it says “app” and select New -> Activity -> Empty Activity
Lesson 11 - Launch a second

A dialog appears where we must define the name of the Activity “Activity Name” and we will assign it as “About” name (two files will be created About.kt and activity_about.xml):

Lesson 11 - Launch a second


We already have the four files needed to implement the application:

Lesson 11 - Launch a second

The first two that are created when we start the project:


And these two new files for the second window:


We implemented the visual interface of the second Activity ie the activity_about.xml file with the following controls:

Lesson 11 - Launch a second

3 – Now we will implement the functionality in the main activity so that the second window is activated.

The source code of the main activity is:

package com.coding180.project010

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
// coding180.com
class MainActivity: AppCompatActivity () {

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

        val button1 = findViewById (R.id.button1) as Button
        button1.setOnClickListener {
            val intent1 = Intent (this, About :: class.java)
            startActivity (intent1)

We create an object of the Intent class and pass it as parameters the object reference of this class (this) and the reference of the other Activity (About:: class.java)
We call later the method startActivity passing the object of class Intent this already has the window of the second Activity on screen):

            val intent1 = Intent (this, About :: class.java)
            startActivity (intent1)

If we run the program we can see that when we press the “About” button, the second window where the TextView is displayed with the value “Robort Gabriel” and a button with the text “Return” (if we press this button nothing happens, this because we have not associated any event to that button)

The source code of the About.Tk activity is:

package com.coding180.project010

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class About : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        var buton1 = findViewById (R.id.button1)
        buton1.setOnClickListener {
            finish ()

When the Return button is pressed, the finish() method is executed, which aims to free the memory space of this activity and request that the previous activity be displayed.

Now our program is fully functional:

First Activity:

Lesson 11 - Launch a second

Second Activity:

Lesson 11 - Launch a second


How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Leave a Reply

Your email address will not be published. Required fields are marked *