How to start a new activity using Intent in Android Studio – Full Code Tutorial and Explanation

What will you learn

  1. Starting another activity using Intent

Note: Find the full code of the project towards the end of the post. Read the entire post for further clarifications and explanations.


Before we get started, let’s take a look at the working application to see what you are going to build.

The working of the Intent App

Building the Interfaces

This is our first application in which we require two activities. We will use the intent function to go from one activity to another.

Start your project with an Empty Activity.

The interface of the first activity

All we need in our first activity is a button, which when pressed, will take us to the next activity. By default, we have the ConstraintLayout set. As we aren’t dealing with a lot of items, we will let it be like this.

  1. Drag and drop a button under the default ‘Hello World’ text.
  2. Set the ID of the button to something you will remember.
  3. Give the button a function name in the onClick option from the properties that you will remember.
  4. You can add constraints to this button, if you don’t remember how to do that, then I highly recommend you go through this article that will help you recollect your memory.

You can find the code to the interface of the activity at the bottom of the article.

Adding a new activity to the project

Next, we need to create the activity ( the screen ) where the button will take us. We can do this in many ways. The easiest one goes like this.

Menu -> File -> New -> Activity -> Empty Activity.

Another really easy way of doing this is from your project sidebar.

App -> Java -> right click on packagename.projectname -> New -> Activity -> Empty Activity

The default settings will name this activity as ‘Main2Activity’.

Adding the new activity in AndroidManifests.xml file

Adding the activity will automatically do this, but if it in some case it doesn’t, then follow these instructions. You do this by opening the AndroidManifest.xml file from the following location.

app -> Manifests -> AndroidManifest.xml

Once here, you need to find two activity tags in between the application tag.

<application>
   <activity></activity>
   <activity></activity>
</application>

If you see the two activity tags, one for each activity, then you are good to go and don’t need to make any changes.

There will be a bunch of other information too.

If you don’t see two activity tags then simply paste this following code below the first activity tag.

<activity android:name=".Main2Activity"></activity>

Unless you have named your second Activity something else.

Now your application recognizes the second activity. But we can’t let it be empty so let’s add some text and a button to come back on the first activity.

The interface of the second activity

The second activity will be completely empty, so we have to add a new text element to show us we are in the second activity, and a button below it to take us back to the previous activity.

  1. Drag and drop a textView element, give it some text like ‘This is the Second Activity’.
  2. Drag and drop a button and give it an ID that you will remember.
  3. Give the button a function name in the onClick option from the properties that you will remember.

You can find the complete code to the interface of this activity at the bottom of this post.

Coding the functionality of the application

You will see that this is by far one of the easiest things to code. Considering that a lot of applications have multiple activities, it shouldn’t be surprising at all. We use two lines of code, each time we want to use the intent.

Coding the java file of the first activity, MainActivity.java

  1. In the first line of code, we describe the intent and give it the source and destination activities.
  2. In the second line, we initialize it.
public void buttonClick(View view) {
    Intent i = new Intent(MainActivity.this, Main2Activity.class);
    MainActivity.this.startActivity(i);
}

Find the entire code of MainActivity.java at the bottom of this post.

Firstly the function name, ‘buttonClick’ is the same name you gave the button in the properties under onClick. Here, we are creating an object ‘i’ of the class intent and then we are passing two parameters. The first one is the location where we are right now. We do that by writing in ‘MainActivity.this’. Then we mention the class name of the activity we want to go to like this ‘Main2Activity.class’.

In the second line, we have to mention the activity we are in, and run the function startActivity and pass the object’i’ that we had created in the first line.

That’s all about it in the first activity.

Coding the java part of the second activity, Main2Activity.java

This is pretty much the same as the code in the first activity. With just the source and destination activities reversed.

public void buttonBack(View view){
    Intent i2 = new Intent(Main2Activity.this, MainActivity.class);
    Main2Activity.this.startActivity(i2);
}

We are doing the same thing as above in this.

  1. Create an object of the intent class.
  2. Add the parameters to the source and destination activities.
  3. Initialize the new activity with the startActivity function.

Find the entire code of Main2Activity.java at the bottom of this post.

Other tutorials you might enjoy

  1. Run audio in your android app using MediaPlayer
  2. How to play a video file using VideoView
  3. How to build a simple calculator app in Android
  4. How to perform arithmetic operations in an Android app

The complete interface code of the Application

Remember to change your Package Name!

The complete java code of the intent application

Remember to change your package name!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.