Bundeeteddee
Bundeeteddee

Reputation: 724

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?

Upvotes: 11

Views: 8133

Answers (5)

Mohit Singh
Mohit Singh

Reputation: 1450

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

Upvotes: 0

Juan
Juan

Reputation: 5589

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.

Upvotes: 0

miniBill
miniBill

Reputation: 1734

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

Upvotes: 0

myo
myo

Reputation: 61

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

Upvotes: 6

Arman
Arman

Reputation: 891

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

Upvotes: 1

Related Questions