Press Back Again To Exit Android Studio Tutorial | How to Exit On Twice Back Press in Android Studio [2021]

 Press Back Again To Exit Android Studio Tutorial- This article will teach you how to implement exit on twice back pressed in android studio using java on your android application.



To implement press back again to exit in android studio, let's first create a project in android studio. 

After that, let's make one boolean variable isPressedOnce and assign false to it. This variable will track if the back key has been pressed in recent time or not, For a time we can use 2 or three-second.

 //create variable as ispressedonce
 boolean isPressedOnce = false;

To make  Exit On Twice Back Press in Android Studio  we have to do some work in the method inside onBackPressed() . Override this method in activity. Then inside this method at first we will check is our variable isPressedOnce is true or false. If it is true we will finish the activity as:

 @Override
    public void onBackPressed() {

        //now if is pressback once is true lets exit
        if (isPressedOnce)
        {
            finish();
        }
        else
        {
            //if ispressedback once not true
            //lets make it true
            isPressedOnce=true;

            //show toast message as press back again to exit
            Toast.makeText(MainActivity.this, "Press back again to exit", Toast.LENGTH_SHORT).show();

            //now we have to make isPressedback variable false in 2 or 3 second
            //lets do it by handler and runner

            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                   //make boolean variable false after 2 sec ie 2000ms
                   isPressedOnce=false;

                   //now lets run app
                }
            },2000);
        }
    }


If variable isPressedOnce is false we need to make this variable true for 2 seconds only then show toast message also as below. Then by using handler and Runnable we can make this variable false so that our app will show  Press Back Again To Exit on back click. Inside runnable method make variable isPressedOnce false. This will make isPressedOnce variable after some sec we have provided. Let's provide 2000ms and then test the application.


If you implement correctly we can see toast message as Press Back Again To Exit while pressing the back key.



Video tutorial for Press Back Again To Exit Android Studio Tutorial



Code snippet: MainActivity.java

package com.roshanaryal.pressbackagaintoexit;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    //create variable as ispressedonce
    boolean isPressedOnce = false;

    @Override
    public void onBackPressed() {

        //now if is pressback once is true lets exit
        if (isPressedOnce)
        {
            finish();
        }
        else
        {
            //if ispressedback once not true
            //lets make it true
            isPressedOnce=true;

            //show toast message as press back again to exit
            Toast.makeText(MainActivity.this, "Press back again to exit", Toast.LENGTH_SHORT).show();

            //now we have to make isPressedback variable false in 2 or 3 second
            //lets do it by handler and runner

            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                   //make boolean variable false after 2 sec ie 2000ms
                   isPressedOnce=false;

                   //now lets run app
                }
            },2000);
        }
    }
}

Please share Press Back Again To Exit Android Studio Tutorial.

Post a Comment

0 Comments