Sindu Potha

Sindu Potha was created by us who wish to enjoy Sri Lankan songs.

Long Back Press Exit Message “Press Again To Exit”

Long Back Press Exit Message "Press Again To Exit" 100% Fix - Android Studio | Best Tech Tutorial
Long Back Press Exit Message “Press Again To Exit” 100% Fix – Android Studio | Best Tech Tutorial

Long Back Press Exit Message “Press Again To Exit”

How to Exit Android App by Double Back Press

Today We are going to discuss How to show Long Back Press Exit Message and Exit Android App by Double Back Press.

Read also:

Long Back Press Exit Message “Press Again To Exit”

Open your Android Studio project and add the following code to your Main Activity.

public void onBackPressed(){
    if (myWebView.canGoBack())
    {
        myWebView.goBack();
    }
    else if (doubleTap)
    {
        super.onBackPressed();
    }
    else
        {
            Toast toast = Toast.makeText(this, "Press Again To Exit",Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0,0);
            toast.show();
            doubleTap = true;
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run()
                {
                    doubleTap = false;
                }
            }, 2000);
    }
}

Add following code to inside the “public class MainActivity extends AppCompatActivity {  here }”

private boolean doubleTap;

Full code MainActivity.java

package com.techedutricks.techedutricks;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private WebView myWebView;
private boolean doubleTap;


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

myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings=myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

//improve WebView Performance
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);

//Load Website
myWebView.loadUrl("https://www.techedutricks.com");
myWebView.setWebViewClient(new MyWebViewClient());
}

private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
if (Uri.parse(url).getHost().equals("techedutricks.com")){
//Content urls open in app
return false;

}else {
//Open external links in external browser or apps
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}


public void onBackPressed(){
if (myWebView.canGoBack())
{
myWebView.goBack();
}
else if (doubleTap)
{
super.onBackPressed();
}
else
{
Toast toast = Toast.makeText(this, "Press Again To Exit",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0,0);
toast.show();
doubleTap = true;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run()
{
doubleTap = false;
}
}, 2000);
}
}
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top