Android 在 loadDataWithBaseURL 中的 onBack 上出现空白屏幕
也许它会有一个简单的解决方案,但我在这里阅读了很多线程,但没有办法。 在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在这个答案中找到了一些信息:
Android:WebView 的方法 goBack() 显示空白页面
需要提供一个 URL 以便在调用时返回。
您传递的是 null,因此默认为“about:blank”。试试这个:
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
You are passing null so defaults to 'about:blank'. Try this instead:
作为解决方法,我进行了以下活动。
通过这种方式,图像会加载到默认的 Android 浏览器中,我可以使用后退按钮将其关闭。
离开 WebView 是不好的,但我别无选择。
As a workaround i made the following activities.
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.
正如 Gadgeteer 提到的,在
loadDataWithBaseURL()
上传递 null 会将'about:blank'
添加到 Web 视图历史记录堆栈中。为了避免这个问题,我在调用
loadDataWithBaseURL()
时立即清除 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 callingas per document
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()试试这个:
而不是你的 loadDataWithBaseURL
Try this:
instead of your loadDataWithBaseURL