1

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())?

Zane Claes
  • 14,732
  • 15
  • 74
  • 131
  • I am not sure if your webview can do something when it's in background. I guess you webview is part of your activity. And when activity is not visible, it's paused. – Martin Vandzura Oct 06 '12 at 21:54
  • I can definitely say that I'm seeing content, upon resuming the activity, that indicates that the webview had "done stuff" in the background. Namely, I see time-sensitive items appear that must have been loaded while the app was paused. – Zane Claes Oct 07 '12 at 07:54

1 Answers1

1

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.

Community
  • 1
  • 1
Radek Pech
  • 3,032
  • 1
  • 24
  • 29