WebView on Android 7.0+ doesn't render page

若如初见. 提交于 2019-12-03 13:48:58

问题


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!


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!