- Home /
Can somebody explain the OnApplicationPause/Focus scenarios?
OnApplicationPause/OnApplicationFocus has been all over the place for me. In the Editor, it seems to be different on PC and MAC. On MAC it seems to get called on press Play. On PC it doesn't. On iOS it seems like one of them doesn't work. Can somebody tell me the different scenarios that they work?
And can somebody explain what they mean in each scenario? Editor? Standalone? iOS? Android? (like does OnApplicationFocus fire when you minimize or when you just click on another window in the Editor)
I know about the documentation, I read the documentation. But the behavior doesn't match it. It's ridiculous. If somebody has figured it out I'd greatly appreciate it.
Totally agree. The behavior of these two functions require trial and error (and possibly better documentation) as they do not behave the same.
I can tell you that I have ended up using only OnApplicationPause
which seems to behave as expected on desktop and iOS (not sure about Android).
Answer by bigdaddy · May 20, 2015 at 07:03 PM
Since this UnityAnswer is one of the first (if not the first) to be returned on a search for OnApplicationFocus/Pause & iOS, an important update in Unity 4.6.1 has changed the behavior for iOS.
As of 4.6.1, both OnApplicationFocus and OnApplicationPause will be called in iOS.
The order is :
App initially starts:
OnApplicationFocus(true) is called
App is soft closed:
OnApplicationFocus(false) is called
OnApplicationPause(true) is called
App is brought forward after soft closing:
OnApplicationPause(false) is called
OnApplicationFocus(true) is called
Hope that helps
You are the most amazing person ever!!! I've spent hours trying to figure this out and even more hours researching! THAN$$anonymous$$ YOU!!!
App initially starts:
OnApplicationPause(false) is called
the code is
when i launch the app the logcat is
Answer by Tortuap · Jan 03, 2019 at 10:10 AM
Btw, tested on Android API 5+ and iOS 12+, using Unity 2018.2, here is what I'm using :
#if UNITY_ANDROID
void OnApplicationFocus ( bool focus )
{
if ( focus ) ResumeApplication ();
else LeaveApplication ();
}
#endif
#if UNITY_EDITOR || UNITY_IOS
void OnApplicationPause ( bool pause )
{
if ( pause ) LeaveApplication ();
else ResumeApplication ();
}
#endif
Why do you treat both devices differently? Do they behave differently?
I like this answer. I prefer to make the "#if UNITY_ANDROID" line to be "#if UNITY_ANDROID && !UNITY_EDITOR" since sometimes I run Android as my platform but in Unity Editor.
Answer by LowerPoly · Dec 15, 2014 at 10:21 AM
OnApplicationPause is what gets called when you soft close on an iOS device. OnApplicationFocus does the same thing but for windows. OnApplicationFocus can be tested inside of the Unity editor if you click out of the game screen for instance it will be called.
I've included a very simple test that I have used for a current game which is built for multiple devices.
void Update()
{
if (Input.GetKey("p"))
{
OnApplicationPause(true);
//OnApplicationFocus(true);
}
}
/*void OnApplicationFocus(bool pauseState)
{
paused = pauseState;
if (paused == true)
{
loginSoftClose.SetActive(true);
}
}*/
void OnApplicationPause(bool pauseState)
{
paused = pauseState;
if (paused == true)
{
loginSoftClose.SetActive(true);
}
}
Answer by atcjavad · Aug 04, 2019 at 01:52 PM
The best answer is in this video https://m.youtube.com/watch?feature=youtu.be&v=_-ssqurlR3A
Your answer
Follow this Question
Related Questions
Execution Order of OnApplicationPause 1 Answer
problem with checking application states on android and windows 0 Answers
Does Unity call Start() when OnApplicationPause(true) 0 Answers
opposite of OnApplicationPause -1 Answers
Android - OnPause() and OnResume() not working with OnApplicationFocus or OnApplicationPause? 1 Answer