开发者

Android: WebView's method goBack() shows a blank page

I have an android application that loads web pages in an activity with a WebView. I am using the retrieving the page manually and using WebView's loadDataWithBaseURL to display it on screen. Everything there is fine.

Now, i am trying to override the Back button press to simulate going back in the WebView history stack. I am able to override the Back button press, i can see开发者_C百科 that there is a history stack in the WebView, i can see that the history url is correct, but when i call WebView's goBack() method, it displays a blank page.

Anyone encountered this before or give me a couple of suggestions to proceed from this?

Edit: If i use WebView's loadUrl method, the Back button with an override works as intended. But why.... If i need to handle this manually, how do i start messing with history pages?


I got the same problem also. I found that the problem went away if I set the historyUrl parameter on the call to loadDataWithBaseURL.


You should check if the canGoBack() method returns true before calling goBack()


The only solution I've found is to create a Stack<String> and manually manage history


The way I deal with this is keeping a local stack pointer to the number of loaded pages after the root page loaded using loadDataWithBaseURL . When going back, if my pointer hits 1 I am at root level and reload the root page with loadDataWithBaseURL.

FYI, I use this code in Activities with fragments, so the fragments implement the interface IBackButtonListener which helps me to capture the back button in the main activity and propagate the event to the current fragment. If the fragment returns true it means it has taken care of the event.

IBackbuttonListener.java

public interface IBackButtonListener {
    public boolean onBackButtonPressed();
}

Fragment that implements IBackButtonListener and has a webview loaded from html data.

    private int historyStackPointer = 0;

    ...

         @Override
            public boolean onBackButtonPressed() {
                boolean rtn = false;

                if (webView.canGoBack()) {
                    if(historyStackPointer > 1) {
                        webView.goBack();
                        historyStackPointer--;
                        rtn = true;
                    }else{
                        if(historyStackPointer == 1) {
                            // Reload the html data 
                            webView.loadDataWithBaseURL("file:///android_asset/", html_data, "text/html", "UTF-8", null);
                            historyStackPointer = 0;
                            rtn = true;
                        }else{
                            webView.loadUrl("about:blank");
                            rtn = false;
                        }
                    }
                } else {
                    rtn = false;
                }
                return rtn;
            }

html_data is a String with the page's html.


What I noticed is that if the url ends in .html, that white screen appears when back button is pressed.

On the other hand, if you remove that .html from your url - obviously only if this is supported by that website (i.e. the redirection and all is handled properly at the server side and that it doesn't trigger the 404 Page Not Found error), that url will act as your base this time and when you press the back button, that white screen should not appear this time.

for example: you have to replace http://example.com/page.html to: http://example.com/page

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜