Issue In Webview Android History When Update Chrome Stable Version 63
My Android app webview works fine with chrome version 61 or 62, but when I update to version 63. My webviewdoes not store the history and webView.canGoBack() always returns false.
Solution 1:
This issue should be chromium's bug. We find out the same issue in our apps. the reason of this issue is we invoke Webview's loadUrl methond in shouldOverrideUrlLoading method, when we do that , webview can't go back in some version of chromium. The code below is my workaround:
publicclassWebViewBugFixDemoextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// todo : set your content layout
final WebView webView = (WebView) findViewById(R.id.webview);
final String indexUrl = "http://www.yourhost.com";
final String indexHost = Uri.parse(indexUrl).getHost();
webView.loadUrl(indexUrl);
webView.setWebViewClient(newWebViewClient() {
publicbooleanshouldOverrideUrlLoading(WebView view, String url) {
if (isSameWithIndexHost(url, indexHost)) {
returnfalse;
}
view.loadUrl(url);
returntrue;
}
@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && request != null && request.getUrl() != null) {
String url = request.getUrl().toString();
if (isSameWithIndexHost(url, indexHost)) {
returnfalse;
}
view.loadUrl(url);
returntrue;
}
returnsuper.shouldOverrideUrlLoading(view, request);
}
});
}
/**
* if the loadUrl's host is same with your index page's host, DON'T LOAD THE URL YOURSELF !
* @param loadUrl the new url to be loaded
* @param indexHost Index page's host
* @return
*/privatebooleanisSameWithIndexHost(String loadUrl, String indexHost) {
if (TextUtils.isEmpty(loadUrl)) {
returnfalse ;
}
Uri uri = Uri.parse(loadUrl) ;
Log.e("", "### uri " + uri) ;
return uri != null && !TextUtils.isEmpty(uri.getHost()) ? uri.getHost().equalsIgnoreCase(indexHost) : false ;
}
}
Solution 2:
Even I was facing this issue. For now You can use intent in ouldOverrideUrlLoading(WebView view, String url){} function and super.onbackpressed() will do its job to maintain history.
publicbooleanshouldOverrideUrlLoading(WebView view, String url) {
Intentintent=newIntent(MainActivity.this, MainActivity.class);
intent.putExtra("postUrl", url);
startActivity(intent);
returntrue;
}
Post a Comment for "Issue In Webview Android History When Update Chrome Stable Version 63"