Autosave on Android

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TobiLa

Hi,

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.NOTIFICATION_WM_QUIT_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.NOTIFICATION_WM_FOCUS_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?

any idea you have to saving you need to acknowledge that saving to a ssd or flash storage(like on mobile phones) wears them out by a bit, so if you save a lot of times you are wearing the memory out(and it will require faster replacement), so unless it is needed for you to save at the exact moment for the next game session it is recommended to not save to the memory and keep working on the ram and not on the storage itself

rustyStriker | 2020-02-12 22:02

Please let me know if you rectified this problem.
Thanks in advance.

sarthakroy | 2020-07-28 18:37