开发者

webview.goBack() method doesnt invoke shouldOverrideUrlLoading method

I want every page lo开发者_如何学运维aded in my app go through the url override method ( where i do some html modifications ). How can i achieve this?.

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // do some html modifcations (myhtml)
    view.loadDataWithBaseURL(url, myhtml, "text/html", "utf-8", url);
    return false;
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
        web1.goBack(); // go back 1 step
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


on page finished method you will get the url and you can reload the url

       @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            _dialog.dismiss();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);
            try{
                _dialog.dismiss();
                Toast.makeText(Showwebviewlatestnews.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
            }catch (Exception e) {

            }
        }


Try to return shouldOverrideUrlLoading true
Here is an explanation:-

True if the host application wants to leave the current WebView and handle the url itself, otherwise return false. Reference

So this should do the trick,

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // do some html modifcations (myhtml)
    view.loadDataWithBaseURL(url, myhtml, "text/html", "utf-8", url);
    return true;
}

And handle backPressed() like -

@Override
public void onBackPressed() {
    if (webview != null && webview.canGoBack()) {
        webview.goBack();
    } else {
        super.onBackPressed();
    }
}


//Replace this

 public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
    web1.goBack(); // go back 1 step
    return true;
}
return super.onKeyDown(keyCode, event);}

With

public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
    web1.goBack(); // go back 1 step
    web1.goBack(); 
    return true;
}
return super.onKeyDown(keyCode, event);}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜