Mihir
Mihir

Reputation: 31

Android Webview, back button does not work when a new link is opened through the webview

I have an android application with local html, css and javasccript files.

One of the pages contains a list of images, these images have a link to an external website attached to them.

Whenever I click on the link, the URL is opened in the webview. But when I click the back button, it does not go back to the previous page.

It works on all the pages except the one where the external link is used.

The MainActivity.java looks like this :

public class MainActivity extends Activity {

WebView mWebView;

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


    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.loadUrl("file:///android_asset/www/index.html");

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.setWebViewClient(new WebViewClient());
    // Force links and redirects to open in the WebView instead of in a browser
}

@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}

the MyAppWebViewClient.java looks like this:

public class MyAppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("/menu")) {
        return true;
    }

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;
}

}

Upvotes: 2

Views: 2218

Answers (1)

Chordin4tion
Chordin4tion

Reputation: 984

See what I have done in my app you can use this as well...

@Override
    public void onBackPressed() {

        if (webView.canGoBack()) {

            webView.goBack();

        } else {
            if (backPressed) {
                super.onBackPressed();
                return;
            }

            this.backPressed = true;
            Toast.makeText(this, "Press Back One More Time To Exit", Toast.LENGTH_SHORT).show();

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    backPressed = false;
                }
            }, 2000);

        }

    }

In this code if your webView can go back it will go back but if it can't a Toast will appear asking user to press back again to exit just to prevent accidental back presses....

Handler.postDelayed() is used to set a time limit of 2 seconds....after two seconds user is prompted again to press back....

Upvotes: 1

Related Questions