[SOLVED] OnApplicationPause() with a save function does not work.

Hello,
So I am developing this game for the android platform, and upon testing my apk I realised that the save and load functionality that worked like a charm in the editor didn’t work. Initially, I thought it was because I used OnDestroy() for the saving, so I switched to OnApplicationPause(). Only to realise that it doesn’t work either.

I have my load function in the “public void Start()” method. And save function in the “public void OnApplicationPause()” method. Is there something wrong with this? This is definitely a matter of calling these save and load functions cuz it executed flawlessly in the Editor.

Please help ;-;

Where do these save/load methods write/read from? I mean, where are you trying to save the data?

Also be aware that functions like OnApplicationPause and OnApplicationQuit may have different behaviour on the various target platforms, as outlined in their documentation, because some platforms treat application sessions very differently.

I would recommend to attach the debugger and check what’s executed at all, then dig deeper…

Hello, thank you for replying. They read and write from “Application.dataPath”. And could you please elaborate on the debugger thing. and advice me on what to do next? Thankyou!

First try not to write to the data path. Instead, use Application.persistentDataPath.

As for the debugger, it’s best to look that up in the docs for the specific platforms.
It’s really easy on the Editor and in standalones, because most IDEs have that functionality built-in, but if you wanna debug a deployed app, you might need to configure / pair any other device first.

Hey, thank you for your help! I fixed this issue by switch to using the OnApplicationFocus() method, because according to the official documentations, this method triggers every time the application loses focus. But, this wasn’t the cause of the issue. Turns out I never even passed in the needed boolean parameter for OnApplicationPause() to work, thats why it didn’t work. As I said, I switch to using the OnApplicationFocus() method as it was more reliable.

public void OnApplicationFocus(bool focusing){
     //if program is not in focus, save
     if(!focusin){
     SaveBySerialisation();
     }
}

Thank you for putting your time into fixing my issue!

3 Likes