简体   繁体   中英

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 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.

你应该在调用goBack()之前检查canGoBack()方法是否返回true

I was having the same problem and I tried all answers but nothing helped (tested all of them on Nexus 5 running Marshmallow). loadDataWithBaseURL solution was not relevant to me as I was using loadUrl instead.

Now, this isn't a solution but something I luckily noticed. This thing is really weird and only works if the target url ends in .html . My intention is to help anyone facing this problem as I know how annoying this can be. So please bear with me, please do not down vote this answer if you think this is nonsense.

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 (ie 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

Again, I am not posting this without testing thoroughly - this works for me today and am happy that I found this and I hope that it helps you too.

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.

我发现的唯一解决方案是创建一个Stack <String>并手动管理历史记录

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM