Skip to content Skip to sidebar Skip to footer

Android Webview "location.replace" Doesn't Work

I have an Android webview with a page that redirects to another page, using location.replace(url). Lets say that I have page 'A' that redirects to page 'B' (using location.replace)

Solution 1:

I work with Yaniv on this project and we found the cause of the problem, it was introduced when we tried to add mailto: links handling according to this answer.

The answer suggested using the following extending class of WebViewClient:

publicclassMyWebViewClientextendsWebViewClient {
    @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {     
        if(MailTo.isMailTo(url)){
            MailTomt= MailTo.parse(url);
            // build intent and start new activityreturntrue;
        }
        else {
            view.loadUrl(url);
            returntrue;
        }
    }
}

The problem was that explicitly telling the WebViewClient to load the URL and returning true (meaning "we handled this") added the page to the history. WebViews are quite capable of handling regular URLs by themselves, so returning false and not touching the view instance will let the WebView load the page and handle it as it should.

So:

publicclassMyWebViewClientextendsWebViewClient {
    @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {     
        if(MailTo.isMailTo(url)){
            MailTomt= MailTo.parse(url);
            // build intent and start new activityreturntrue;
        }
        else {
            returnfalse;
        }
    }
}

Solution 2:

functionlocationReplace(url){
  if(history.replaceState){
    history.replaceState(null, document.title, url);
    history.go(0);
  }else{
    location.replace(url);
  }
}

Solution 3:

Try this way..

publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebViewmainWebView= (WebView) findViewById(R.id.webView1);

        WebSettingswebSettings= mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebViewClient(newMyCustomWebViewClient());
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        mainWebView.loadUrl("file:///android_asset/www/A.html");
    }

Or get help from this and this link

Post a Comment for "Android Webview "location.replace" Doesn't Work"