Search code examples
javascriptandroidback-buttongoogle-forms

webview loadUrl stops working AFTER controls on google forms get the focus


I was trying to create a kiosk-wrapper for a bunch of google forms surveys and thought that maybe that was a good opportunity to get a taste of android development (my first attempt that is).

the problem

I'm overriding the onBackPressed() in order to allow the user to jump to my homepage (an index page with links to various surveys) when I realized that once the user had already given focus to some input field on the survey form, I was no longer able to load the homepage (loadUrl).

I couldn't replicate this behavior with the input fields in other sites such as google.com. Therefore my guess is that google forms uses some complex javascript(?) and this somehow messes up with subsequent loadUrl calls? But it being my first attempt in java or android, I'm ashamed to admit that I'm groping in the dark.

My code that exhibits the problem

I've trimmed down the code exhibiting the issue to the bare minimum. Both loadUrl() calls, open a sample google form that I've created. I'd be so glad if somebody could point out my mistake and give me some lead as to how to overcome this issue!

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    protected FrameLayout webViewPlaceholder;
    protected WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize the UI
        initUI();
    }

    protected void initUI()
    {
        // Retrieve UI elements
        webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));

        // Initialize the WebView if necessary
        if (webView == null)
        {
            // Create the webview
            webView = new WebView(this);
            webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            webView.getSettings().setSupportZoom(true);
            webView.getSettings().setJavaScriptEnabled(true);


            // Load the URLs inside the WebView, not in the external web browser
            webView.setWebViewClient(new WebViewClient());

            // Load a page
            webView.loadUrl("https://docs.google.com/forms/d/e/1FAIpQLSdkbLwUPSPL1fx1oL8iMyLkBiMY591qjZ-xwCEf15fyX3ikYw/viewform");
        }

        // Attach the WebView to its placeholder
        webViewPlaceholder.addView(webView);
    }

    @Override
    public void onBackPressed() {
        webView.loadUrl("https://docs.google.com/forms/d/e/1FAIpQLSdkbLwUPSPL1fx1oL8iMyLkBiMY591qjZ-xwCEf15fyX3ikYw/viewform");
    }

Behavior

As it is, it will load the sample survey form. If you press the android UI back button, it will reload it (for simplicity in this example I use the same url as a homepage). If you navigate forward (2nd step of the survey) the android UI back button will take you to the cover page, indicating that indeed loadUrl() worked. But if on the 2nd step you give focus to the text field (or the radio buttons), the android UI back button will stop functioning.

Errors

Everytime I press the UI back button (regardless of whether it works or not, I get the following error):

W/UnimplementedWebViewApi: Unimplemented WebView method onKeyDown called from: android.webkit.WebView.onKeyDown(WebView.java:2310)

Before stripping down the code to the bare essentials, I'd get the next message instead:

Webview loadUrl won't work after user focuses on google forms form controls (error InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient)

I've found multiple posts on both error messages, but nothing that I could relate to. Any help would be so much appreciated!


Solution

  • Funny thing how a good night's sleep can help! For what it's worth, I tried the following and it works now: Before reloading a new url with loadUrl() I disable javascript and re-enable it as soon as I load the new page!

    webView.getSettings().setJavaScriptEnabled(false);
    webView.loadUrl("https://docs.google.com/forms/d/e/1FAIpQLSdkbLwUPSPL1fx1oL8iMyLkBiMY591qjZ-xwCEf15fyX3ikYw/viewform");
    webView.getSettings().setJavaScriptEnabled(true);
    

    The above code is the onBackPressed() body