Search code examples
javascriptandroidandroid-webviewandroid-websettings

Phone screen turns white after clicking back from Android Webview


I am using Android Webview to load some web content into my app. The loading page has some Javascript content, so I have to set the ' webSettings.setJavaScriptEnabled(true)" as true, the problem is when i click back from the webpage whole phone screen turns to white blank screen.

I found one more thing is if i set webSettings.setJavaScriptEnabled(false) the webview loads the page successfully, but not fully shows the content as expected. I am using the Code below. How can i get rid of this issue ? Searched so many places and couldn't find any helpful answer.

  private void initView() {
    initWebView();
    initWebSettings();
}

@SuppressLint("NewApi")
private void initWebView() {
    setWebChromeClient(new WebChromeClientCustom());
    setWebViewClient(new WebViewClientCustom());
    setOnLongClickListener(this);
    setFocusableInTouchMode(true);
    requestFocus();
}

@SuppressLint("SetJavaScriptEnabled")
private void initWebSettings() {
    final WebSettings webSettings = getSettings();
    webSettings.setUseWideViewPort(false);
    webSettings.setLoadWithOverviewMode(false);
    webSettings.setSupportMultipleWindows(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(false);
    webSettings.setAppCacheEnabled(true);
}

Solution

  • If you want to close Activity that holds WebView when back pressed, you can override onBackPressed method and close the activity:

    @Override
    public void onBackPressed() {
        finish();
    }