1

I want to store the data exactly when Application.Quit() method get called. Basically I want one callback method that get called one time during application life cycle, just when application killed by operating system of the device.

With iOS, I managed to work with following line of code:

void OnApplicationQuit()
{

    //if (pauseStatus)
    //{
    DataStorage.StoreLastOpenedDay(DateTime.Now.Day);
    DataStorage.StoreLastOpenedMonth(DateTime.Now.Month);
    DataStorage.StoreLastOpenedYear(DateTime.Now.Year);
    //}
}

But I got information that in Android, OnApplicationQuit method didn't get called by the running activity.

So what is alternative exist in Android for implementing same thing as like iOS? I can't use OnApplicationPause method because it get called multiple times when any external process happen when your application is running for ex. phone call, in app purchase related popups etc... I want code to be executed only once when application get killed.

Please share your side suggestions about this.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Siddharth
  • 4,142
  • 9
  • 44
  • 90
  • Gave an upvote to the question after seeing a downvote, completely undeserved. I wonder sometimes why people downvotes legit questions. – Galandil Feb 17 '18 at 16:13

1 Answers1

4

On Android, Application.Quit() calls OnApplicationQuit(), but only from inside the Unity runtime.

This means that if you have the application paused in background, and the OS decides to kill it, OnApplicationQuit() will not be called at all.

Basically, to ensure that your app saves the data, you'll need to juggle with OnApplicationPaused() and OnApplicationFocus(), but this means that you won't be able to perform the save exactly once per lifetime of the app.

Galandil
  • 4,169
  • 1
  • 13
  • 24
  • Also I have tried with placing code just above Application.Quit() but mostly Android user pressing Home button of device so in this condition too, event not get called :( – Siddharth Feb 17 '18 at 14:50
  • Exactly, so your best bet as I said in my answer is to save your data everytime `OnApplicationFocus(bool hasFocus)` has `!hasFocus`. – Galandil Feb 17 '18 at 15:11
  • After doing proper research in multiple places, I can say that at present there is no way exist to detect application kill / destroy in Android - For my solution, I have implemented another way to handle same thing, On application start I have managed my code. – Siddharth Feb 19 '18 at 14:30