Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnApplicationPause & OnApplicationFocus get delayed

Discussion in 'Android' started by antique, Sep 1, 2015.

  1. antique

    antique

    Joined:
    Mar 8, 2015
    Posts:
    5
    I want to save the current level progress when player exits my game, so I did something like this:
    Code (CSharp):
    1.  
    2.     void OnDisable()
    3.     {
    4.         Debug.Log("OnDisable");
    5.         SaveLevel();
    6.     }
    7.  
    8.     void OnApplicationQuit()
    9.     {
    10.         Debug.Log("OnApplicationQuit");
    11.         SaveLevel();
    12.     }
    13.  
    14.     void OnApplicationFocus(bool focusStatus)
    15.     {
    16.         Debug.Log("OnApplicationFocus");
    17.         SaveLevel();
    18.     }
    19.  
    20.     void OnApplicationPause(bool pauseStatus)
    21.     {
    22.         Debug.Log("OnApplicationPause");
    23.         SaveLevel();
    24.     }
    However, sometimes when I exit the game none of the functions above gets called, so the app is terminated without saving anything. Is that a bug or I'm missing something? The quick workaround would be to save the game every 30 seconds or so, but I would prefer to do it in a cleaner way.
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    Are you sure the object you've attached this script to exists in the scene when you exit the game ?
     
  3. antique

    antique

    Joined:
    Mar 8, 2015
    Posts:
    5
    No, the object is never deleted. I also observed that if the above functions don't fire immediately they are called about 15 seconds after closing the game to background with unity message about forcing the engine to pause.
     
  4. TimGS

    TimGS

    Joined:
    Apr 24, 2014
    Posts:
    70
    How exactly do you exit application when this function doesn't get called? From unity script or by terminating it while it's in background?
     
  5. antique

    antique

    Joined:
    Mar 8, 2015
    Posts:
    5
    I exit by pressing button to show all running apps and then swiping my game (just like a normal user would do). Neither
    OnApplicationPause or OnApplicationFocus gets invoked unless I wait about 5 seconds. After that time Unity writes
    "Timeout while trying to pause the Unity Engine" to logcat and calls these functions. I tested this using the latest version of unity (5.2). Additionally, I'm using Soomla Store, Admob + Unity ads and CloudOnce plugin.
     
  6. bisongun

    bisongun

    Joined:
    Feb 21, 2014
    Posts:
    2
    Did anyone get resolution to this issue? We're actually having the same problem (OnApplicationPause, Focus are not called and eventually Unity Timeouts). We're also using Soomla, and Unity 5.2.2.
     
  7. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    We have one bug reported that may be related to this. If you sync your app to a target frame rate below 40fps you may hit pause timeouts. Could that be the issue you are seeing?
     
  8. bisongun

    bisongun

    Joined:
    Feb 21, 2014
    Posts:
    2
    Ah, that's mostly likely it. We target 30fps. Should we submit an additional bug report on this? Also, thanks so much for your timely reply!
     
  9. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
  10. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Hi,

    It seems like the "Timeout trying to pause Unity" warning trigger the OnApplicationPause function even if we don't pause anything.

    We also have the target fps to 30. It can be related ?

    Our game trigger OnApplicationPause randomly and we were asking why.