Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap keep running on Android after onPause

I decided to test if phonegap WebView is live when app is in a background.

function test2(x){
    $('<div></div>', {text: x.cmd}).appendTo($('#list'));
}
$(function(){test2({cmd:"start"});});
function cb_pause(){test2({cmd:"pause"});}
function cb_resume(){   test2({cmd:"resume"});}
function tick(){test2({cmd:"timer" + timer});timer++;setTimeout(tick, 1000);}
document.addEventListener("pause", cb_pause, false);
document.addEventListener("resume", cb_resume, false);
tick();

It prints pause and resume on onPause and onResume. and print timer0, timer1, etc every second.

It was printing even when I left app and started playing game. I know I can and should stop timer in onPause but still. It looks wrong. I expected android will freeze WebView.

I know background thread will not stop if in background app until it is unloaded.

So question is: Is there way to freeze/suspend/unload webview on onPause and unfreeze/resume/load on onResume

When I say load I mean it should be in same state when user left app.

When I say freeze, I mean that my code to will pause even when it will continue to call setTimeout

I guess I expect Android to behave like iOS.

like image 838
Max Avatar asked Feb 02 '14 00:02

Max


2 Answers

I am late with response. In phonegap there is KeepRunning param in config.xml

You must add following line to config.xml

<platform name="android">
    ...
    <preference name="KeepRunning" value="false" />

It will call pauseTimers() and pause all WebViews. pauseTimers() is not hidden method and no reflection is needed.

like image 150
Elena Avatar answered Oct 20 '22 12:10

Elena


This should help: WebView threads never stop (WebViewCoreThread, CookieSyncManager, http[0-3])

Basically you have to use reflection to access the WebView's onPause/onResume methods to really make it pause and resume.

like image 27
DataDino Avatar answered Oct 20 '22 12:10

DataDino