Zane Claes
Zane Claes

Reputation: 14954

Android WebView consumes lots of power when the application is running in the background

I have a WebView inside my android app, and this WebView is running a website with a fair bit of Javascript on it. Users have reported high power consumption when my app is running in the background, and I expect it is due to this javascript. However, I don't want to completely unload or remove the WebView, as this would hurt the time-to-resume.

Is there any way to selectively turn off Javascript and/or disable the WebView completely when the app is in the background (onPause())?

Upvotes: 1

Views: 1131

Answers (1)

Radek Pech
Radek Pech

Reputation: 3098

Accoring to http://www.mcseven.me/2011/12/how-to-kill-an-android-webview/ the only working way is to redirect to empty page with no javascript (and return back after resume).

In case of PhoneGap, the page should contain a code to return itself:

<!DOCTYPE html><html><head>
        <script type="text/javascript" src="js/phonegap/cordova-2.9.0.js"></script>
        <script type="text/javascript" src="js/jquery/jquery-1.9.1.min.js"></script>
    </head><body>
        <script type="application/javascript">
            $(document).on('deviceready', function() {
                $(document).on('resume', function() {
                        return history.back ? history.back() : history.go(-1);
                });
            });
        </script>
    </body></html>

Update: The above is for Cordova 2.x.x. In Cordova 3.x.x (not sure if since 3.0.0, but for sure in currect 3.7.x) you can simply add KeepRunning preference in your configuration XML:

<preference name="keepRunning" value="false" />

This will pause all JS timers until the APP is resumed again; see https://stackoverflow.com/a/21629586/2011448.

Upvotes: 1

Related Questions