英文:
How to make to webview returns to the previous page and does not show about: blank?
问题
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
} else {
finish();
}
super.onBackPressed();
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
loadErrorPage();
}
private void showErrorLayout() {
mlLayoutRequestError.setVisibility(View.VISIBLE);
}
private void loadErrorPage() {
if(mWebView!=null){
String htmlData = "<html><body><div align=center>Check your internet!</div></body></html>";
mWebView.loadUrl("about:blank");
mWebView.loadDataWithBaseURL(null, htmlData, "text", "utf-8", null);
mWebView.invalidate();
}
}
例如,加载了Google页面,然后出现错误,加载了about:blank,当重新加载页面时,WebView会重新加载about:blank,而不是Google页面。如何在重新加载时使Google页面加载?
英文:
P.S my English very bad and I have mistakes in text))) please sorry!
How to make to webview returns to the previous page and does not show about: blank?
here is my code:
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
else {
finish();
}
super.onBackPressed();
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mbErrorOccured = true;
showErrorLayout();
super.onReceivedError(view, errorCode, description, failingUrl);
loadErrorPage();
}
}
private void showErrorLayout() {
mlLayoutRequestError.setVisibility(View.VISIBLE);
}
private void loadErrorPage() {
if(mWebView!=null){
String htmlData ="<html><body><div align= center >Check your internet!</div></body>" ;
mWebView.loadUrl("about:blank");
mWebView.loadDataWithBaseURL(null,htmlData, "text", "utf-8",null);
mWebView.invalidate();
}
}
For example, the Google page loaded, then an error occurred and about: blank was loaded, and when the page is reloaded, the WebView reloads about: blank and not the Google page. How to make the Google page load on reload?
答案1
得分: 0
如果已经加载了about:blank页面,然后通过调用webView.reload()
重新加载webview,它只会重新加载当前页面,即about:blank。
如果您想要加载先前的页面,只需调用webView.goBack()
,或者您可以直接加载网址,如下所示-> webView.loadUrl("<your.web.url>")
如果您希望更好地控制webview,可以使用WebViewClient和WebChromeClient。
请参阅下面的代码示例:
首先创建WebViewClient并将其分配给webview。
var currentUrl: String = "https://www.google.com"
webview.webViewClient = MyWebViewClient()
在WebViewClient的shouldOverrideUrlLoading
方法中,您可以有自己的逻辑来决定要加载哪个网页。
class MyWebViewClient : WebViewClient() {
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
super.onReceivedError(view, request, error)
// 在这里显示带有重试功能的自定义错误消息
displayErrorDialog()
}
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
// 更新当前网址
if (url != null) {
currentUrl = url
}
return super.shouldOverrideUrlLoading(view, url)
}
}
英文:
If about:blank page is already loaded and then you are reloading webview by calling webView.reload()
, it will just reload the current page i.e. about:blank.
If you want to load previous page, just call webView.goBack()
or you can directly load the url like this -> webView.loadUrl("<your.web.url>")
Use WebViewClient and WebChromeClient if you want to have a better control on webview.
See the code below
First create WebViewClient and assign it to webview.
var currentUrl : String = "https://www.google.com";
webview.webViewClient = MyWebViewClient()
In WebViewClient's shouldOverrideUrlLoading
method, you can have you own logic to decide which web page you want's to load.
class MyWebViewClient : WebViewClient(){
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
super.onReceivedError(view, request, error)
// here display your custom error message with retry feature
displayErrorDialog()
}
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
// update current url
if (url != null) {
currentUrl = url
}
return super.shouldOverrideUrlLoading(view, url)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论