- Home /
Unity Android Suspend/Resume and Quit behaviour
Can someone explain (or point me to a document explaining ) the Suspend/Resume, Focus/Unfocus and Application Quitting flow for a Unity android application?
What I'm trying to do is detect when the user pauses or quits my app, and save their data so they can resume from the same place later.
From my experimentation, I've found that iOS apps behave in the way you would expect; OnApplicationFocus and OnApplicationPause are called at appropriate points when the app is put into the background and resumed, and OnApplicationQuit is called when the user kills the application.
However Android apps behave in a significantly different fashion;
Application Starts
OnApplicationFocus(true) is called (Gain focus)
User presses the Home or Running Apps button which puts the current app into the background
OnApplicationPause(true) is called (Pausing)
User selects the app from the background apps menu to make it the current app again
The app re-starts entirely, rather than resuming from where it left off, as if the user had booted it for the first time
The user presses the power button to turn off the screen
OnApplicationPause(true) is called (Pausing)
OnApplicationFocus(false) is SOMETIMES called (Lose focus)
The user presses the power button to turn on the screen again
OnApplicationFocus(true) is SOMETIMES called (Gain focus) (if focus was previously lost)
OnApplicationPause(false) is called (Resuming)
The user kills the app from the running apps menu
OnApplicationQuit() IS NOT CALLED
Is this expected behaviour? It seems as though, on Android, apps are never really in the background, since they have to restart each time the user "resumes" them. The puzzling one is why OnApplicationQuit is never called. Is the only way around this behavior to save the game each time OnApplicationPause(true) is called and load up the most-recent save data each time the application boots?
Answer by Bunny83 · Aug 12, 2016 at 10:29 PM
Well, the Android system has a quite complex system for managing apps in memory. The point "6" is not true unless the OS has decided it needed the memory and kicked your app out of memory. In this case your App has to restart entirely, yes. If it's still in memory you can resume where you left the game.
When you press the power button the app usually should not loose focus and therefore "OnApplicationFocus" is not called. When the user turns the device back on your app should still have the focus.
AFAIK "OnApplicationQuit" is only called on Android when you specifically quit the App via Application.Quit()
. Calling Application.Quit will throw the app out of memory manually. I haven't tested this recently but it used to be that way.
Some people are confused by Androids "recent apps" screen. This screen simply contains "recent apps". Some apps might still be in memory and can be resumed, others might have been killed already and have to be restarted if you select it. The OS can decide on it's own when it kicks an app out of memory. This can happen to any app that is in the background at any time. Usually the OS has to free some memory when you start another app that requires the memory.
So whenever "OnApplicationPause(true)" is encountered you might want to store your state. The app might get killed in the meantime.
When your app starts up you want to check your stored state if you can resume the game. Likewise when OnApplicationPause(false) is encountered you also want to resume the game.
Your answer
Follow this Question
Related Questions
portability Android to IOS/Windows phone 0 Answers
desktop,ios and android script differences?? 2 Answers
Load images seamlessly from internal storage in unity android? 0 Answers
Does receipt verification require an internet connection? 0 Answers
Handheld.PlayFullScreenMovie() works on Android but not iOS? 2 Answers