11

I am using webview in Android. But strangely, sometimes even webview canGoBack method returns true, webview goBack method doesn't work.

if (webView.canGoBack()) 
    webView.goBack();

Thanks for any idea.

cagryInside
  • 790
  • 2
  • 15
  • 41

3 Answers3

12

I finally managed to figure out how to do it

@override
public void onFormResubmission(WebView view, Message dontResend, Message resend)
{
  resend.sendToTarget();
}

default behaviour of onFormResubmission is not to resubmit. resend.sendToTarget() changes that.

this will make your hardware back button work if you have made some code to handle the hardware button

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
  if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
    mWebView.goBack();
    return true;
  }
  return super.onKeyDown(keyCode, event);
}
miniBill
  • 1,743
  • 17
  • 41
ram
  • 3,487
  • 10
  • 33
  • 47
  • 5
    I am using the above code and having issue of method mWebView.canGoBack() always returns true even if there is no history. The problem is I can not go back to previous activity when pressing device back button because mWebView.canGoBack() returns true. Any help? – AndroidDev Jun 07 '12 at 07:47
  • this fixed the issue i was having. – jph Oct 02 '13 at 19:41
4

I have disabled the cache and then it worked:

WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
codejunkie
  • 41
  • 3
-2

use this one windows.history.back();

Nagaraja
  • 581
  • 1
  • 4
  • 12