Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by madflyskills · Jul 19, 2013 at 02:44 PM · editoronapplicationpauseonapplicationfocus

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DannyB · Jul 19, 2013 at 03:31 PM 1
Share

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).

4 Replies

· Add your reply
  • Sort: 
avatar image
34

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Valdeco · Nov 05, 2015 at 07:15 PM 6
Share

This should be on Unity documentation

avatar image NinjaISV · Jan 31, 2017 at 08:25 PM 0
Share

You are the most amazing person ever!!! I've spent hours trying to figure this out and even more hours researching! THAN$$anonymous$$ YOU!!!

avatar image $$anonymous$$ · Apr 22, 2017 at 04:33 AM 2
Share

App initially starts:

OnApplicationPause(false) is called

the code is alt text

when i launch the app the logcat is alt text

1.jpg (78.0 kB)
2.jpg (15.6 kB)
avatar image
3

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
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image waldgeist · Apr 20, 2019 at 01:21 AM 0
Share

Why do you treat both devices differently? Do they behave differently?

avatar image alex_roboto · Jul 10, 2020 at 04:18 AM 0
Share

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.

avatar image
2

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);
         }
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xtro · Feb 12, 2018 at 09:36 PM 0
Share

This is a better answer.

avatar image joseGuate97 · May 05, 2020 at 01:29 AM 0
Share

What about android? Which one is the best option?

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges