Search Unity

Bug OnApplicationPause/Focus not being triggered after game is on background for a long time MOBILE

Discussion in 'Scripting' started by NiteDev_, Jan 31, 2023.

  1. NiteDev_

    NiteDev_

    Joined:
    Oct 11, 2014
    Posts:
    19
    Hello, someone has some info related to this, we have some games made for mobile, and we use the Pause/Focus callbacks to make a data reload from our server, but when the app is on background for a long time, both callbacks are not working, neither Android or iOS, so, does someone knows how this can be achieve? making the callbacks works after a long soft close of the game ... Thx for the help :)
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    What exactly does that mean? Those callbacks can only fire when the app is in the foreground. Unity is a foreground application so no code will run when the app is in the background. Also doing server request in OnApplicationPause can easily fail since it's an asynchronous process which by default can not be completed since the application is already in the background when a response may arrive your device.

    An app in the background on Android is essentially suspended. However the OS can decide to kill any background app at any time in case the memory is needed. So if an app is in the background of a long time, chances are high that it got killed, so your app would actually restart when you switch back to it.

    You may want to add some information what your exact observations are and what you're actually doing inside those callbacks, In any case you should store relevant data locally in case the app goes into the background.