Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Go Back to Previous Activity in Android?

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 25 Nov, 2022
Improve Article
Save Article

In this article, we are going to see how we can add a back button to an activity through which we can go back to its previous activity. This can be achieved with just a few lines of code, which is explained in the steps below

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the activity_main.xml File

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""
        android:text="Next Activity"
        app:layout_constraintTop_toTopOf="parent" />

Step 3: Create a New Activity

In the projects folder, right-click on app > new > Activity > Empty Activity. You can customize this activity accordingly.

Creating New Activity to Navigate


Step 4: Working with MainActivity File

We now add an onClickListener in the MainActivity to trigger whenever the button is clicked and set up an Intent inside it.


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    // create Button object   
      Button button;
    protected void onCreate(Bundle savedInstanceState) {
          // give reference to the Button view whose id is button 
        // and give it to the Button instance created above
        button = findViewById(;
          // setting onClickListener
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // create an intent to switch to second activity upon clicking
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);


import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
class MainActivity : AppCompatActivity() {
    // create Button object
    private lateinit var button: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        // give reference to the Button view whose id is button
        // and give it to the Button instance created above
        button = findViewById(
        // set onClickListener
        button.setOnClickListener(View.OnClickListener {
            // create an intent to switch to second activity upon clicking
            val intent = Intent(this,

Step 5: Checking AndroidManifest.xml

Navigate to app > manifest > AndroidManifest.xml

Adding Parent Attribute to Manifest File


Step 6: Check Parent Attribute

Inside Android Manifest File, Check if SecondAcitvity has been added or not, and then add a new attribute of parentActivityName and set it to MainActivity as shown below.


<?xml version="1.0" encoding="utf-8"?>
        <!-- Shall be automatically added after creating new activity.
            If not, add the following code snippet -->
        <!-- till here -->
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Now run the Application. In the second activity, the back button at the top left can be used to go back to the previous activity.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!