Android WebView goBack and goForward not working with WebViewClient

I used WebViewClient to display web pages in a webview, and also added back and forward buttons for navigation, but it doesn’t work, whenever I press the back and forward buttons, it doesn’t go, and the expected URL stays at the same url

Please help me. I need it

+3
source share
2 answers

I disabled the cache and then it worked:

 WebView webView = (WebView)findViewById(R.id.webView);
 webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
+2
source

Here is the 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;
         }
    }
}
+1
source

Source: https://habr.com/ru/post/1780379/


All Articles