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

后端 未结 5 1363
一整个雨季
一整个雨季 2021-01-11 16:38

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 loadDataWithBaseUR

相关标签:
5条回答
  • 2021-01-11 16:55

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

    0 讨论(0)
  • 2021-01-11 17:07

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

    0 讨论(0)
  • 2021-01-11 17:14

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

    0 讨论(0)
  • 2021-01-11 17:17

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

    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.

    0 讨论(0)
  • 2021-01-11 17:19

    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.

    0 讨论(0)
提交回复
热议问题