I have WebView screen with license. And everything worked perfectly until users notified me that nothing shows on Android 7+ devices.
public class DefaultWebActivity extends AppCompatActivity {
WebView mWebView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.default_web);
mWebView = (WebView) findViewById(R.id.web_view);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.w("WebActivity", "Error loading page " + description);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return true;
}
});
mWebView.loadUrl("https://google.com/");
}
@Override
protected void onPause() {
super.onPause();
mWebView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mWebView.onResume();
}
}
I can see that it loads something(google url appears in shouldOverrideUrlLoading method) but it shows nothing. No error logs appear.
This code perfectly works on <7.0 android version devices. I have read that Android 7+ uses Chrome to render screen but I didn't fount what I have to add to fix issue.
Thanks in advance!
From Android doc
This is pre Android N
@Deprecated
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
This is since Android N
- @return True if the host application wants to leave the current WebView
- and handle the url itself, otherwise return false.
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return true;
}
This method is from Android N , so for this reason you have this issue only in Android N. Returning false you should solve your problem.
来源:https://stackoverflow.com/questions/42367358/webview-on-android-7-0-doesnt-render-page