I'm developing a game which should be saved automatically so that the player get's to the same point on restarting my app, no matter when he closes the app.
I have two basic ideas for this:
Approach 1: Save when user closes the app
The easiest would be to save when the user closes the app.
I know of two ways closing the app in android:
Back button: This is easy, as I can save when I get the MainLoop.NOTIFICATIONWMQUIT_REQUEST
The problem I have is when the user is minimizing the app and swiping it out of the recent apps. I get a notification MainLoop.NOTIFICATIONWMFOCUS_OUT but there seems to be only a short execution time, because my code after this isn't executed anymore. Is there a way to save my progress when the user closes the app like this?
Approach 2: Save everytime there is something to save
I implemented this approach until now, but it's not working that great.
When saving in my Main Thread that often during gameplay I am afraid that it wouldn't do the performance any good and doing file operations that are 99% of the time overwritten a few ms after that.
I also tried to save in a Thread, but its hard in my case to avoid that some part of my saved progresses is edited while saving, which leads to other problems. Using Mutexes in this case would be the same as doing the saving in the main thread, since it's not practical for me to split up my save data.
So what is the common approach for it? How would you do it?