What happens to data state when the user leaves my mobile hybrid app?

67 Views Asked by At

I am making a hybrid mobile app using Ionic for both Android and IOS. I was wondering: what happens to your app when the user leaves it (a.k.a. clicks on the home button to go to the phone's menu)? Can code still run in the background?

Is there a way to run code as a response to the user leaving the app? What about when the user shuts down the app, do I have a small window of time to run code?

I am asking these questions because I would like to make sure that my app's state management isn't messed up as a result of the user leaving/shutting down my app. For example, I would like to save unsaved data to the database if the user shuts down the app, and save data locally if the user leaves the app (i.e. if the user is filling out a form but leaves midway, I wouldn't want him/her to have to start over).

Thank you!

1

There are 1 best solutions below

2
On

When the user leaves an app, it goes into the background. I'm not an expert, so I'd recommend checking the official iOS and Android docs for more details.

Capacitor: Check out the Background Task API and you also can listen for when the app goes into the background with the Capacitor App API.

How to go about ensuring that state is saved varies per app. Generally, you should assume that your app could be killed at any point, and act accordingly. Thus, my suggestion is to save data after every major user action. Check out the photo gallery tutorial in the Ionic docs. There, each time the user takes a photo, it's saved to the filesystem, and app state is saved too.