1

I have a Web View with a single URL but in that URL, contents are clickable which can further load other contents (Note: its the same link, with new contents). Now, when i click on the back button, i should go to the previous contents instead of coming out of the screen (which is the default behavior of back button). Please note here that, since we don't have new URL links when we click on some contents inside the webview, so

        if (webView.canGoBack()) {
            webView.goBack();
        } 

won't help.

There is some javascript code in web html page which helps to control back press for multi contents:

String script = "component.faq.goBack()";
        webView.evaluateJavascript(script, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {
                Timber.e("LogName : " + s);

            }
        });

I used the above code inside onBackPressed() method it worked but always return null String in onReceiveValue callback, So there is no way to distinguish if its the top level content or some inner content (In case of inner content, it should go to the previous web content which it is going but in case of top level web content it should go back the previous screen).

Full onBackPressed() method is something like this:

@Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            String script = "component.faq.goBack()";
            webView.evaluateJavascript(script, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {
                    Timber.e("LogName : " + s);

                }
            });
            //super.onBackPressed();
        }
    }

Rephrasing the problem again, in the above code i can go to previous content in the Webview on back press click but not sure where & how to place super.onBackPressed(); so that on top webView content, i can go to previous screen (String in onReceive() is always null).

Does anyone have some trick or solution for this issue.

Thanks in advance!

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
  • What is the return type of `component.faq.goBack()`? Thats the function that the callback is using ([check here](http://stackoverflow.com/questions/19788294/how-does-evaluatejavascript-work)) – Bonatti Sep 04 '15 at 11:33

0 Answers0