Having problems getting the "Back" button to work in webview.
I'm not sure where in the code I should put the onKeyDown()
part (currently under the last @Override
)
Both mywebview.canGoBack()
and mywebview.goBack()
result in errors saying "mywebview cannot be resolved".
If I move the bracket after mywebview.setWebClient(...)
all the way to the bottom, mywebview-error
goes away, but then override
, onKeyDown
, return true
, and return super
result in all kind of errors.
Can anyone tell me what to do?
Code:
package com.sib;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SiB extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sib);
WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("url");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
mywebview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}