Android 在 loadDataWithBaseURL 中的 onBack 上出现空白屏幕

发布于 2024-12-28 14:56:28 字数 947 浏览 2 评论 0原文

也许它会有一个简单的解决方案,但我在这里阅读了很多线程,但没有办法。 在 ListView 中,如果我点击一行,它将打开一个新的活动。在该活动中,我创建了一个 httpget,并使用该 httpget(检索到的网页的一部分)中需要的内容创建了一个 html 字符串。

所以我只是做了一个loadDataWithBaseURL("http://base_path.com/", html, mime,encoding, null)

它按预期工作,我查看带有链接和图像的网页。 现在问题来了......如果我点击图像,我会在该窗口中看到大图像,但是一旦我按手机上的“后退”,我会看到一个白色页面。我知道这是由“null”参数引起的,但是......我应该放什么才能再次看到html页面?我尝试将“html”而不是 null,但我在 webview 中看到了 html 代码!

这是我的 onKeyDown 来覆盖后退按钮:

@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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

油焖大侠 2025-01-04 14:56:28

我在这个答案中找到了一些信息:
Android:WebView 的方法 goBack() 显示空白页面

需要提供一个 URL 以便在调用时返回。

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

您传递的是 null,因此默认为“about:blank”。试试这个:

String baseUrl = "http://base_path.com/";
loadDataWithBaseURL(baseUrl, html, mime, encoding, baseUrl + filename);

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);
夜访吸血鬼 2025-01-04 14:56:28

作为解决方法,我进行了以下活动。

  1. 删除了 onKeyDown 函数
  2. 删除了 myWebView.setWebViewClient(new WebViewClient());

通过这种方式,图像会加载到默认的 Android 浏览器中,我可以使用后退按钮将其关闭。

离开 WebView 是不好的,但我别无选择。

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.

壹場煙雨 2025-01-04 14:56:28

正如 Gadgeteer 提到的,在 loadDataWithBaseURL() 上传递 null 会将 'about:blank' 添加到 Web 视图历史记录堆栈中。

为了避免这个问题,我在调用 loadDataWithBaseURL() 时立即清除 webview 历史记录,方法是

webview.clearHistory()

按照文档

调用

public voidclearHistory()

告诉此 WebView 清除其内部后退/前进列表。

如果在调用 loadDataWithBaseURL() 之后调用clearHistory(),则 webview 不会返回到空白页面,并且 canGoBack() 也会返回 false;因为在clearHistory()之后webview中没有返回堆栈

As Gadgeteer mentioned passing null on loadDataWithBaseURL() will add the 'about:blank' into the stack of webview history.

To avoid this problem, I clear webview history as soon as I call loadDataWithBaseURL() by calling

webview.clearHistory()

as per document

public void clearHistory ()

Tells this WebView to clear its internal back/forward list.

If you call clearHistory() after calling loadDataWithBaseURL() the webview not go back to blank page and canGoBack() also return false; because there is no back stack in the webview after clearHistory()

爱本泡沫多脆弱 2025-01-04 14:56:28

试试这个:

webview.loadUrl("file:///android_asset/your_html.html");

而不是你的 loadDataWithBaseURL

Try this:

webview.loadUrl("file:///android_asset/your_html.html");

instead of your loadDataWithBaseURL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文