I used WebViewClient to display web pages in webview and also added back and forward buttons to navigate but it does not work, whenever i click back and for开发者_如何学Goward buttons it does not go intended url instead remains in same url
Please help me. I am need of it
I have diabled the cache and then it worked:
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Here is a code that works for me :
package debut.stackoverflow;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class StackoverflowActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView webview = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient(new HelloWebViewClient());
((Button)findViewById(R.id.buttonBackward)).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
webview.goBack();
}
});
((Button)findViewById(R.id.buttonForward)).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
webview.goForward();
}
});
webview.loadUrl("http://www.google.com");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
}
精彩评论