Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android blank screen on onBack in a loadDataWithBaseURL

Probably it will have a simple solution but I've read many threads here but no way. In a ListView, if i tap on a row it opens a new Activity. In that activity I make an httpget and I create an html string with what I need from that httpget (a portion of the web page retrieved).

So I simply make a loadDataWithBaseURL("http://base_path.com/", html, mime, encoding, null).

It works as expected and I view the web page with links and images. Now the problems come... If I tap on an image I see the large image in that windows but once i press the "back" on the phone I see a white page. I know that it is caused by the "null" argument but... what I should put to see the html page again? I tried to put "html" instead of null but I see the html code inside the webview!

This is my onKeyDown to override the back button:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the BACK key and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    // If it wasn't the BACK key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}
like image 351
Vale Avatar asked Jan 23 '12 15:01

Vale


2 Answers

I found some information at this answer: Android: WebView's method goBack() shows a blank page

You need to provide a URL to back into when you call

loadDataWithBaseURL("http://base_path.com/", html, mime, encoding, null);

You are passing null so defaults to 'about:blank'. Try this instead:

String baseUrl = "http://base_path.com/";
loadDataWithBaseURL(baseUrl, html, mime, encoding, baseUrl + filename);
like image 82
Gadgeteer Avatar answered Dec 15 '22 07:12

Gadgeteer


As a workaround i made the following activities.

  1. removed the onKeyDown function
  2. removed the myWebView.setWebViewClient(new WebViewClient());

In this way the image loads in the default Android browser and i can close it with the back button.

It is not fine to go outside the WebView but i had no other choices.

like image 36
Vale Avatar answered Dec 15 '22 06:12

Vale