Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView on Android 7.0+ doesn't render page

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!

like image 381
Dmytro Avatar asked Feb 21 '17 12:02

Dmytro


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

Is there an alternative to Android System WebView?

Flyperlink is a great alternative app to System WebView. System WebView is the browser that is used when you open a link from within an app. It's a very basic version of Chrome that many people get very frustrated with. There are no customization options for System WebView.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

Is Android WebView deprecated?

Beginning October 5, 2021, Facebook Login will no longer support using Android embedded browsers (WebViews) for logging in users.


1 Answers

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.

like image 75
an_droid_dev Avatar answered Sep 30 '22 13:09

an_droid_dev