Resume event not firing when clicked on icon

With Android, when the app is opened and closed by pressing the backbutton the ‘pause’ event is fired… Thats correct, as the app keeps running in the background.

But many users re-open the app again by clicking on the app icon… In this case the app starts a new instance, and the ‘paused’ app keeps running in the background, cause I can see the Cordova plugin throwing double logcats…

Also I tried to save a JS variable, that remembers its state, but the variable always return false (meaning its the first launch). So its a complete new Ionic app instance.

The ‘resume’ event is also not fired (only the deviceReady)… So I end up with 2 app instances…

It does work correct when I re-open the app from the ‘active apps overview’… Then the resume event is fired and there is only 1 app instance.

Is there something I am missing?

I might be wrong but there is always 1 app instance running at a point as the bundle identifier is the same. the logcat showing 2 logs can be because of controller etc. initializing twice. How do you know that there are 2 instances running? Do you see it in the android device manager or you are basing it on logging?

the place where you are saving the state variable might not be persistent so it is reset every time the app starts.

Post some code here if possible or create a codepen.

Hi Gaurav_ch

Thanks for the quick reply…

I base it on the fact that my Java cordova camera plugin starts again, while it has a static variable check for isInit true / false… If the Java code would be running in the same scope, it would have seen it was created before, and should not do anything else than restart the RTSP stream… But when I resume the app, it restarts from beginning, breaking the other camera instance (there can only be one focus on the camera)…

So my previous ‘paused’ instance starts throwing errors, and another instance says everything is fine, thus there should be 2 instances… Also, if it would resume the ‘background’ instance, it should fire the cordova resume event, which it does not…

When I re-open the app through the ‘active apps overview’ in Android, everything works fine… The cordova resume event is triggered, the java code sees its been init before and continues as programmed.

Very simplified code, not much more going an then triggering the cameraStream:

 <script type="text/javascript">

  var started = false;

function startStream() {
//var textValue = document.getElementById("myText").value;
cameraStream.startStream("HOI");
}

function onDeviceReady() {
console.log("Device ready");

console.log('started: ' + started);

started = true;

startStream();
}

function onDevicePause() {
  console.log("Device pause");
}

function onResume() {
 console.log('Device onResume');
}

function onBackButtonPressed() {
  console.log('Device backButtonPressed');
}

function endCallButtonPressed() {
console.log('Device backButtonPressed');
}

document.addEventListener("deviceready", onDeviceReady, false);

document.addEventListener("pause", onDevicePause, false);

document.addEventListener("resume", onResume, false);

document.addEventListener("backbutton", onBackButtonPressed, false);

document.addEventListener("endcallbutton", endCallButtonPressed, false);
</script>

From the most simple perspective, I created a global JS variable in the index.html, called ‘started’… But it always returns false when opening from the home-screen icon… Shoudn’t that log ‘true’ when returning after being paused?

AFAIK the pause event will stop the execution of js code and will reset the app to start from beginning when resumed after some time. This arbitrary because if I tap on home and immediately tap on the icon, my app works fine but if I tap on the icon after a minute, my app restarts. I also have an app which uses onpause and onresume event and I always store the state, js var in localstorage and read from there onresume.

The code you shared will always return started false as the js execution is reset. I know I am not being helpful at all but this is what I understand without looking at the actual full code as there might be some plugins also which could create the problem and Android is prone to that a lot in my experience.

No worries gaurav,

I appreciate you take time to respond…

I did some testing, and when the app does ‘resume’ as it should, the ‘started’ variable logs true… So javascript is kept in memory after pause (for a limit time), .

In my best knowledge I can understand why the app ‘restarts’, a native app does the same after a while, if the phone has to free up memory and removes the processes from its stack… But what I don’t understand, is why a native app can resume, where Ionic can not. While the app still has a minimum memory footprint at the time of writing. And it also doesn’t explain why the app icon has a different bootstrapping route then the ‘active apps overview’… Both trigger (like you mentioned) the same ‘app’… So why one always restarts, and the other mostly resumes, remains a bit strange… The resume event is simply never triggered when clicking on the icon, while going through the other route does it correctly.

If I find time I will debug Ionic from the inside out, see if I can find some clues… Otherwise I just throw Ionic away and try a complete vanilla Cordova app, if that does’t work, CocoonJS is going to get a chance… Otherwise just back to native

Thanks