Loading...

Implementing Navigation Back Feature to Android WebView Application

By default, Navigation Back Feature is disabled in android WebView. You can enable it by Overriding onKeyDown method, so in this tutorial I will teach you how to Implement Navigation Back Feature to Android WebView. To get back from your android WebView application, you need to press back button from android device.

Android WebView straight back Button over-ride is very important whenever your program is loading a page or even inner site. You may possibly well be traveling the site navigation tree however after you hit on the Android straight back it shuts your own program. Well here's really a cure for it.

You may execute Webview using a preceding and following buttons within this Android Webview Straight Back Button Navigation In Androidstudio tutorial.

If you'd like to load the total internet site in WebView, subsequently navigation switches really are all useful.


The next code may assess in case you've traveled a minumum of one degree down the locally-stored internet site. In case yes it'll throw back you it's webpage, else the button will serve as ordinary.

Android Example: Implementing Navigation Back Feature to Android WebView Application

Implementing Navigation Back Feature to Android WebView Demo:

 

Use Following Code to Implement Navigation Back Feature:


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack(); // Go to previous page
            return true;
        }
        // Use this as else part
        return super.onKeyDown(keyCode, event);
    }




Step 1: Create Android Project:

Create a new android project for Implementing Navigation Back Feature to Android WebView Application with following information:

Application name: Adding Navigation Back Feature to Android WebView
Company Domain: sirseni.com
Package name: com.sirseni.addingnavigationbackfeaturetoandroidwebview
Minimum SDK: Android 2.2 (API 8 Froyo)

Step 2: Adding WebView to Your activity_main.xml file:

res/layout/activity_main.xml

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myWebView"
    android:scrollbars="none"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


Step 3: Modify your MainActivity.java file:

src/MainActivity.java

/* Implementing Navigation Back Feature to Android WebView */
package com.sirseni.addingnavigationbackfeaturetoandroidwebview;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {
    WebView myWebView;

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

        myWebView = (WebView) findViewById(R.id.myWebView);
        myWebView.loadUrl("http://www.centerend.com");
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new MyWebViewClient());

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.centerend.com")) {
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack(); // Go to previous page
            return true;
        }
        // Use this as else part
        return super.onKeyDown(keyCode, event);
    }
}


Add the INTERNET Permission to Your AndroidManifest File

You must add the INTERNET permission to your AndroidManifest.xml file to load website in your application. So before running your WebView application, add single line code to your Android Manifest file.

<uses-permission android:name="android.permission.INTERNET" />


Step 4: Modify AndroidManifest.xml file:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sirseni.addingnavigationbackfeaturetoandroidwebview">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Now run your Implementing Navigation Back Feature to Android WebView application by just clicking Run icon. To see how Navigation Back Feature works, just click one of home screen icon like HTML, CSS, Bootstrap and for back to home page you need to press back button from your android device.

Simple Android WebView Example and Tutorial

How to Load HTML Data/String on Android WebView Application

Implementing Navigation Back Feature to Android WebView Demo:




Download Complete Example Project


Download complete Implementing Navigation Back Feature to Android WebView Application example project source code from GitHub.
WebView 772400878275161521
Home item