Close Android App Programmatically

In this tutorial, I m going to demonstrate how to close android app programmatically with help of a small android application project. You will come across different methods of closing android application programmatically but I m going discuss only those who work for me.

How To Close Android or Exit Android App Programmatically

Step: 1

Create a new project in Android studio with default settings and configuration and name it CloseAppTutorial.

Step: 2

Create three activities MainActivity, ActivityB, and ActivityC. We will test close/exit functionality from different activities. Below is the code of each activity and its related layout.

Open your MainActivity layout file and update the code as given below

Let’s add some code in our MainActivity. MainActivity is the launcher activity.

We have two different ways of closing Android application. It depends on the number of activities we have in the stack. If there is only one activity in the stack then we can call finish() method to close or exit the application.

Finish() method closes the current activity as we don’t have any other previously opened activity in the stack. It is the first activity, therefore, it will close the application.

If we have more than one activity in the stack, finish() method will only close the current activity and we will be landed on the previous activity. It will not close the application.

In order to demonstrate closing or exiting the android application with multiple opened activities in the stack, we have created the third activity that is ActivityC.

Here is the code of our second activity that is ActivityB.

Code of layout file of ActivityB. We have two Buttons and a TextView.

ActivityC is the third activity. Below is the code of ActivityC

Code of layout file for ActivityC.

Project Structure:

close android app programmatically

Now we are ready to test the application. Run the application and navigate to the ActivityC. We are in the third activity. We have three activities in the stack MainActivity, ActivtyB, and ActivtityC. If we call finish() method then it will only close the ActivityC and user will be moved to ActivityB.

In order to close all activities and exit the application, we need to use FLAG_ACTIVITY_CLEAR_TOP flag. It will clear all activities in the stack.

This piece of code will clear all activities and close the application. We are passing “CloseApp” to main activity through Intent.

In MainActivity if we receive the “CloseApp” through Intent then we call the finish() method to close the application. FLAG_ACTIVITY_CLEAR_TOP flag has already cleared the other activities from the stack and we have only one activity that is MainActivty.

Video Demo:

This video is showing the project structure and application out.

Some Usefull Android Development Toturials

I hope you will like this article. Subscribe to our Newsletter to get new articles and tutorials in your inbox as soon as they published. You can connect with us on Facebook and also subscribe to our Youtube Channel.

Check Also

Kotlin SQLite Database Tutorial With Example

A step by step tutorial for implementing SQLite database in Kotlin. I will implement all …