Open In App

How to Finish All the Previous Activities in an Android Application?

Last Updated : 17 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Android applications are having so many activities present inside them for different functionalities. When a user performs some action within the application he will be navigated from one screen to another. When the user is navigated to the next screen the previous activity remains open in the stack. As the user opens so many activities the stack size of activity increases and this increases the memory consumption of the application. To reduce the memory consumption of the application we have to close all the previous activities within the android application. In this article, we will take a look at How to finish all the previous activities in an Android application. 

Note: This Android article covered in both Java and Kotlin languages. 

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.

Step 2: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/idRLContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--on below line we are creating
        a text for our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/idBtnOpenActivity2"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Activity 1"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--on below line we are creating a button-->
    <Button
        android:id="@+id/idBtnOpenActivity2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:text="Go to Activity 2"
        android:textAllCaps="false" />
  
</RelativeLayout>


Step 3: Creating a new activity

Navigate to app>java>your app’s package name>Right click on it>New>Empty Activity>Name it as >Main2Activity and click on Finish to create a new activity. 

Step 4: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    // on below line we are creating a variable.
    lateinit var openActivity2Btn: Button
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // on below line we are initializing our variables.
        openActivity2Btn = findViewById(R.id.idBtnOpenActivity2)
  
        openActivity2Btn.setOnClickListener {
            // on below line we are opening a new activity
            val intent = Intent(applicationContext, MainActivity2::class.java)
              
            // on below line we are starting a new activity
            startActivity(intent)
              
            // on below line we are finishing MainActivity
            finish()
        }
    }
}


Java




package com.gtappdevelopers.kotlingfgproject;
  
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // on the below line we are creating a variable.
    private Button openActivity2Btn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // on below line we are initializing variables with ids.
        openActivity2Btn = findViewById(R.id.idBtnOpenActivity2);
  
        // on below line we are adding click listener for our button
        openActivity2Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on below line we are opening a new activity
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                  
                // on below line we are starting a new activity
                startActivity(intent);
                  
                // on below line we are finishing MainActivity
                finish();
            }
        });
    }
}


Step 5: Working with the activity_main2xml file

Navigate to app>res>layout>activity_main2.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">
  
    <!--on below line we are creating
        a text for our app-->
    <TextView
        android:id="@+id/idTVHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:padding="10dp"
        android:text="Activity 2"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />
  
</RelativeLayout>


Now run your application to see the output of it. 

Output:

In the below output video when we click on Go to Activity 2 button user is navigated to Main2Activity and the previous activity is cleared. As we press back instead of navigating back to MainActivity the application is closed because the MainActivity is already being removed from the Activity Stack. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads