How to close an Android application gracefully?


Introduction

In many android applications we can see that when the application is being closed an animation is added to it which will close the application by displaying a closing animation to it. This will make the application user interface better. In this article we will take a look on How to close an Android application gracefully ?

Implementation

We will be creating a simple application in which we will be creating a text view in which we will be displaying the heading of our application. After that we will be creating a button which we will be using to close the application. Now let’s move towards android studio for creating a new project in Android studio.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 2: Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layoutDirection="ltr"
   android:paddingTop="8dp"
   android:paddingBottom="8dp"
   tools:context=".MainActivity">

   <!--     creating a text view on below line-->
   <TextView
       android:id="@+id/idTVHeading"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:layout_marginStart="20dp"
       android:layout_marginTop="20dp"
       android:layout_marginEnd="20dp"
       android:layout_marginBottom="20dp"
       android:padding="4dp"
       android:text="Close Application gracefully in Android"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />

   <!-- creating a button for closing the application on below line -->
   <Button
       android:id="@+id/idBtnClose"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idTVHeading"
       android:layout_centerHorizontal="true"
       android:layout_margin="16dp"
       android:backgroundTint="@color/purple_500"
       android:text="Close Application"
       android:textAllCaps="false" />
</RelativeLayout>

Explanation: In the above code we are creating a root layout as a Relative Layout. Inside this layout we are creating a text view which is used to display the heading of our application. After that we are creating a button which we will be using to close our application by adding a closing animation to it.

Step 3: Creating a new animation file for adding an animation

Navigate to app>res>Right click on it>New>Resource Directory and name it as anim. After that, right click on that directory and create a new anim file and name it as animation.xml. After creating the file add below code to it. Comments are added in the code to get to know in detail.

  <?xml version="1.0" encoding="utf-8"?>
<!-- on below line creating an animation and adding duration to that animation -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="1000"
   android:fromAlpha="1.0"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:toAlpha="0.0" />

Explanation: In the above code we are creating an animation by creating an alpha. IN this we are firstly specifying the duration for our animation in milli-seconds. After that we are creating a from alpha and then two alpha. Then we are adding an interpolator to it.

Step 4: Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.java_test_application;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
   // creating variables for buttons and relative layout.
   private Button closeBtn;
   private RelativeLayout relativeLayout;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       //initializing variable for relative layout and button on below line.
       closeBtn = findViewById(R.id.idBtnClose);
       relativeLayout = findViewById(R.id.idRLLayout);
       // on below line adding click listener for our button to close the application.
       closeBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line creating a variable for animation and adding the animation to it which we are creating on below line.
               Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation);
               // on below line adding animation listener for the animation which have created.
               anim.setAnimationListener(new Animation.AnimationListener() {
                   // on below line creating on animation start method
                   @Override
                   public void onAnimationStart(Animation animation) {
                   }
                   // on below line creating an on animation end method in which we will be closing our application.
                   @Override
                   public void onAnimationEnd(Animation animation) {
                       // on below line closing our application by calling the finish method.
                       finish();
                   }
                   // on below lie creating on animation repeat method which will be called when animation is repeated.
                   @Override
                   public void onAnimationRepeat(Animation animation) {
                   }
               });
               // on below line we are adding animation by calling start animation method for our relative layout
               relativeLayout.startAnimation(anim);
           }
       });
   }
}

Explanation: In the above code firstly we are creating variables for buttons and a relative layout which is our root layout. Now we will get to see the onCreate method. This is the default method of every android application. This method is called when the application view is created. Inside this method we are setting the content view i.e the layout file named activity_main.xml to set the UI from that file. Inside the onCreate method we are initializing the button and relative layout variables with the ids which we have given in our activity_main.xml file. After that we are adding an on click listener for our close application button. Inside the on click listener method we are creating an animation for our activity which we will apply when the application closes.

In this animation we are passing the animation which we have created. After that we are adding an animation listener for our animation. Inside this we will get to see several methods such as onAnimation start which will be called when the animation is started, onAnimation end which will be called when the animation is completed. In this method we are calling the finish method to close the application. After that we are creating an onAnimationRepeat method which will be called when we have to repeat the animation. Lastly we are adding the animation to our relative layout and starting our animation by calling start animation method.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above article we have taken a look on How to close an Android application gracefully ?

Updated on: 08-May-2023

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements