1

Am working on Android studio,Initially I opened my url within webview,I want to provide a back button and when button is onclicked it has return to current webview page i.e source of url in webview.

ski
  • 45
  • 6
  • @ski check this post [Java bridge Android](http://programmerguru.com/android-tutorial/binding-javascript-and-android-code-example/) – Kathi Apr 05 '16 at 12:37

3 Answers3

1

WebView have a method called goBack(), but this should be called if your WebView allows you to go back via canGoBack(). You should override both onBackPressed() and onKeyDown() and there use this method. Something like this

Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { 

        //if Back key pressed and this WebView has a back history item.
        webView.goBack(); 
        return true; 
     } 
  return super.onKeyDown(keyCode, event);
} 
Aurelian Cotuna
  • 3,076
  • 3
  • 29
  • 49
  • How to provide a custom button icon along with external URL in web-view, If I on-click the icon it should redirect to web view page where it got navigated. – ski Apr 05 '16 at 12:38
  • You can use a `RelativeLayout` with a `WebView` and a `Button` in your xml. `RelativeLayouts` allow you to place views on top of each other and also provide a powerful way to arrange views inside (relative to other views or parent edges). After you do that, you can set click listeners on button and control the webview like that – Aurelian Cotuna Apr 05 '16 at 12:45
0

from that , Back Navigation in a Webview Android App

@Override public void onBackPressed() {
    if (web.copyBackForwardList().getCurrentIndex() > 0) {
        web.goBack();
    }
    else {
        // Your exit alert code, or alternatively line below to finish
        super.onBackPressed(); // finishes activity
    } }
Community
  • 1
  • 1
come
  • 11
  • 1
0

on Click of back Button You add this

     if (mWebView.canGoBack()) {
                mWebView.goBack();
            } 
Arjun saini
  • 4,223
  • 3
  • 23
  • 51