Search code examples
javaandroidandroid-activitywebview

Webview activity calls onDestroy() when loses focus


I've a simple activity with a webview:

public class AGenericWebView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_generic_web_view);
        WebView webView = findViewById(R.id.mainWebView);
        ImageButton backButton = findViewById(R.id.backButton);

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        webView.getSettings().setJavaScriptEnabled(true);
        Bundle intentBundle = getIntent().getExtras();

        webView.getSettings().setSaveFormData(false);

        TextView title = findViewById(R.id.title);
        title.setText(intentBundle.containsKey("title")
                ? intentBundle.getString("title")
                : getResources().getString(R.string.navigazione));

        webView.setWebViewClient(new WebViewClient());
        WebSettings settings = webView.getSettings();
        settings.setSaveFormData(true);
        settings.setDomStorageEnabled(true);
        if (intentBundle.containsKey("cookie") && intentBundle.containsKey("url")) {
            final String cookie = intentBundle.getString("cookie");
            final String url = intentBundle.getString("url");
            CookieManager.getInstance().setCookie(url, cookie);
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
            webView.loadUrl(url);
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int progress) {
                    setProgress(progress * 100);
                }
            });
            webView.setWebViewClient(new WebViewClient());

        } else if (intentBundle.containsKey("html")) {
            String htmlData = intentBundle.getString("html");
            webView.loadData(htmlData, "text/html", "UTF-8");
        } else if (intentBundle.containsKey("url")) {
            String url = intentBundle.getString("url");

            if (intentBundle.containsKey("postdata")) {
                String postData = intentBundle.getString("postdata");
                byte[] postDataBytes = EncodingUtils.getBytes(postData, "BASE64");
                webView.postUrl(url, postDataBytes);
            } else
                webView.postUrl(url, null);
        }
    }
}

When the user switchs between applications, i.e. to copy the OTP from an authenticator (and then the activity loses focus) the activity calls onDestroy(), so he have to repeat the login procedure.

How can I do to keep the webview activity alive?


Solution

  • In the end I found the problem. I forgot to have entered in the manifest for the webview activity

    android:noHistory = "true"
    

    which involves the destruction of the activity in case of loss of focus (the webview was not involved).