[Solved] Ionic in the background


#1

So I’ve made myself a small “incremental game”.

Note: For those who are not familiar, you click on a button and it increases a value, a currency of sorts. You buy upgrades to autogenerate this currency after you’ve laboriously made enough by clicking/tapping manually, saving you the cost of buy a new mouse. Or a new phone, in this case.

Now, I’ve been testing around with it. New to Ionic and all. I’ve realized that on the android (I did Ionic run android), if I hit the native “back” button on the phone, it pauses the app completely. My autogenerating is suspended. That’s fine and all! It’s intended after all.

The issue/problem comes when I hit the native home button. Which brings me to the home screen on an android as well as when my phone auto sleeps. The feature that is intended to save battery life. Whenever either of this happens, the autogenerating continues in the background.

Coming from native android, java. I understand there’s a lifecycle to apps. How is Ionic’s like? And does it go into different states depending on method of exiting the app?

Extra info: I’m currently using $interval to keep the game’s main loop running. I autogen my currency once per second as well as save it into an sqlite database right after. (Both running at once per second).

EDIT1: I have no plugins installed! Using only services. :slight_smile:


#2

Are you running the following plugin?


#3

Hi there! Thanks for asking. I am not running any plugins in the background at all. It seems that the app is paused only when the back button is hit (bringing me out of the app), which is the correct behavior that I am looking for.


#4

Okay. I’ve sort of figured that all this has something to do with the lifecycle. From what I know using my debugging tool, I’m tracking the app doing these two, “Resume” and “Pause” by typing in my controller:

document.addEventListener("resume", function() {
    console.log("Resume!");
  }, false);

  document.addEventListener("pause", function() {
    console.log("Pause!");
  }, false);

I’ve figured out my issue using this. So I know that regardless of method of exiting the app, I will encounter “pause” and regardless of from which method you exit the app, it passes through “resume” state on the way back in. So using this two, I simply cancel my interval when it pauses and start it up again when it resumes!