Pause/Resume events - How to save application state?

Hey guys,

I don’t understand what actually happens when app is being paused in background and how to handle resume event. All I can find on the internet are super simple examples that console.log or alert “app resumed” when app resumes but no real world example of how to restore aplication state.

I have these questions:

  1. When app is paused and then after some time resumed, does it always return to the same view and controller? Or can it resume at home screen and I need to programatically navigate back where it was before?

  2. If it resumes in the same place where it was paused, what happened to my data? Is it restored or do I need to do it manually myself?

  3. If I need to do it manually myself, how exactly do I save and restore my application state? Do I need to save content of all my variables to local storeage and then populate them back after resume event?

Sorry for the long question but I honestly don’t know how to handle this and I can’t find any real example. Your help would be much appreciated. Thanks.

Hi @lrolecek,

When an application is paused, it is frozen in the background. By frozen I mean that all data, views, ect are kept in tact until it is resumed.

Take the following example:

You are on screen 3 with x, y, z variables loaded. If you pause and then resume, you will see screen 3 with x, y, z still loaded.

Now this happens without touching the pause and resume listeners. You would use the pause and resume listeners if you want special tasks to happen. For example, say you want to password protect your app. In the resume listener you might have it redirect to a login state rather than just unfreeze your app. You could also have your app download fresh data in the resume listener. Possibilities are endless.

Hopefully this makes sense.

Regards,

Great, thanks a lot. That’s how I thought it should work but I wasn’t sure.

I actually have $interval ticking in my app that I need to stop when app is paused, I’m trying to do that but it seems that even though I call $interval.cancel() in my pause event handler, when app resumes it quickly goes through all $interval ticks that it “missed” when paused. But that’s most probably some bug in my code.

Anyway, thanks again for your prompt answer.

If the user starts another app while an app is in background and the OS get short in memory starting the new app the OS looks for apps in the background. These apps are removed to create more free memory. So it is not always guaranteed that an app in the background can be recovered to its original state. If you want the app recovered then you must write some cide to save state onPause and rebuild state onResume if the app restarts.

@pcr,

Good point. I forgot about that.

@Pcr, that’s exactly what I’m exeriencing. Got any tips or links to articles how to rebuild the state? I’m afraid that as a newbie I’ll create some horribly unreliable code if I try to invent something myself :slight_smile:

This is a pretty serious problem for us. Your app could be left in an unusable state where the user has to force quit the application. Is there any active development to solve this issue at this time?