Search code examples
androidhtmlwebviewhttp-equiv

HTTP-EQUIV="REFRESH" isn't working on android web view


I have a web page with a basic form (like a newsletter subscription).

The user sends its information and gets a success page with a link to the form submission page. If he doesn't click on the link the page will be automatically redirected after 5 seconds.

<meta http-equiv="refresh" content="5;url=www.site.com" />

So far so good! It works as expected in both desktop and mobile browsers.

Then I created an android app (min API SDK 21) with a web view to load the site.

The problem is that the auto refresh isn't working inside the web view...

Am I missing something? I'm trying to avoid the javascript hack...

Thanks for your time!


EDIT

I notice that when I click on the new form submission page link it opens the default browser instead of rendering it on the web view!


Solution

  • After searching for the problem, I came to a solution by resetting the URL on URL loading.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        ...
    
        mWebView = findViewById(R.id.webview);
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mWebView.loadUrl(url);
                return true;
            }
        });
        mWebView.loadUrl(url);
    }
    

    It's working, but I think it can be improved.

    My initial problem was that I wasn't setting a WebViewClient.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        ...
    
        mWebView = findViewById(R.id.webview);
        mWebView.loadUrl(url);
    }
    

    Thus, I think I just need to add a WebViewClient and it'll work

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        ...
    
        mWebView = findViewById(R.id.webview);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl(url);
    }
    

    Note, and if we want javascript to work we need to enable it

    mWebView.getSettings().setJavaScriptEnabled(true);