Reputation: 314
My Android app webview
works fine with chrome version 61 or 62, but when I update to version 63. My webview
does not store the history and webView.canGoBack()
always returns false
. But previous versions of chrome work fine. How to solve?
Upvotes: 2
Views: 1285
Reputation: 221
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:
public class WebViewBugFixDemo extends AppCompatActivity {
@Override
protected void onCreate(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(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (isSameWithIndexHost(url, indexHost)) {
return false;
}
view.loadUrl(url);
return true;
}
@Override
public boolean shouldOverrideUrlLoading(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)) {
return false;
}
view.loadUrl(url);
return true;
}
return super.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
*/
private boolean isSameWithIndexHost(String loadUrl, String indexHost) {
if (TextUtils.isEmpty(loadUrl)) {
return false ;
}
Uri uri = Uri.parse(loadUrl) ;
Log.e("", "### uri " + uri) ;
return uri != null && !TextUtils.isEmpty(uri.getHost()) ? uri.getHost().equalsIgnoreCase(indexHost) : false ;
}
}
Upvotes: 2
Reputation: 31
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.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("postUrl", url);
startActivity(intent);
return true;
}
Upvotes: 0