4

In my android app, a web view wraps a mobile website. As a user browsers the mobile website I would like to show/hide back and/or forward arrow to assist his/her navigation. To determine show hide back and forward arrows I check webview canGoBack() and canGoForward() in webviewClient's onPageFinished() (see code below). Below is the problem.

Navigation sequence - Page1, Page2 and Page3. OnPageFinished() fired only on Page1, not Page2 and Page3.

I also tried to add WebChromeClient to webview and capture onProgressChanged(), but onProgressChanged only fired on Page1 and Page3.

As a result I can only show back arrow on page3 instead of Page2. On Page3, when back arrow was clicked onPageFinished() on Page2 fired.

I don't have access to mobile web and couldn't get any help on mobile web. Base on viewing source code of html I found a hyperlink is clicked on Page1 to get to Page2.

My question is why onProgessChanged() on Page2 and Page3 didn't fire when navigating in the order of Page1, Page2 and Page3, but onProgressChange() fired on Page2 when backing out.

Any help from you will be highly appreciated. If you need more detail please let me know.

@Override protected void onCreate(Bundle savedInstanceState) {

    ...
    wv = (WebView) findViewById(R.id.webView);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewHandler());
    wv.setWebChromeClient(new WebChromeHandler()); 
    wv.loadUrl(urlPassedIn);
    ...

}

private class WebViewHandler extends WebViewClient {

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        int visible = wv.canGoBack() ? View.VISIBLE : View.INVISIBLE;
        navBack.setVisibility(visible);
        visible = wv.canGoForward() ? View.VISIBLE : View.INVISIBLE;
        navForward.setVisibility(visible);
    }


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

}

private class WebChromeHandler extends WebChromeClient
{
    @Override
    public void onProgressChanged(android.webkit.WebView view, int newProgress)
    {
        super.onProgressChanged(view, newProgress);

        if(newProgress == 100)
        {
           //inject java code here
            int visible = wv.canGoBack()? View.VISIBLE: View.INVISIBLE;
            navBack.setVisibility(visible);
            visible = wv.canGoForward()? View.VISIBLE: View.INVISIBLE;
            navForward.setVisibility(visible);
            wv.loadUrl("javascript:( function () { var resultSrc = (document.querySelector('#content .product-detail') !=null); window.callout.directionCallback(resultSrc); } ) ()");
        }
    }

}

Josh
  • 41
  • 4

0 Answers0