Search code examples
iosswiftout-of-memoryappdelegateuiapplicationdelegate

Questions about app state after tapping on app icon after apps enters suspended state


I’m putting an app into background.

  1. Assuming that I’m not doing anything to keep the app alive in the background, then the app goes through suspended state in a matter of 5 seconds. Right?

  2. What happens if I then tap on the app icon? That’s not suppose to trigger a didFinishLaunch right? It will just bring me back to the last screen I was at and also trigger didbecomeActive & willenterforeground notifications. I won't be getting any other callback. Right?

  3. Assuming there is no restart of the phone, point 2 is true even if there are hours between me tapping home and then tapping back the app icon. Right? Does it also persist device restarts but not force-restart?
  4. The only time I won’t be brought back to the screen I was at (before hitting home) is if the device receives a memory warning and my app is flushed out of suspended state. At this point tapping on the app icon will result in didFinishLaunch. Right?

(I’m asking all of this because sometimes after putting the app in the background and tapping the app icon again (e.g. 10 minutes later), the app is going through it’s launch phase. Most of the time it just goes back to its previous screen)

I've already seen Will ios terminate the app running in background after a specific time? but that doesn't address all aspects I want.


Solution

  • It will just bring me back to the last screen I was at and also trigger didbecomeActive & willenterforeground notifications.

    Right, if your app was not terminated in the background.

    I won't be getting any other callback.

    Not necessarily true. If you were summoned to the front by a local notification, for example, you'll also get an event about that.

    Assuming there is no restart of the phone, point 2 is true even if there are hours between me tapping home and then tapping back the app icon.

    Not necessarily. The app might well be terminated silently in the background.

    Does it also persist device restarts but not force-restart?

    Absolutely not. How can the app run when the device is off? Shutting down the app terminates every app.

    I’m asking all of this because sometimes after putting the app in the background and tapping the app icon again (e.g. 10 minutes later), the app is going through it’s launch phase

    It's not a matter of time. The watchdog process is constantly combing the suspended apps looking for the ones that take up too much memory so that other apps can run. You must not be surprised if yours is one of them.

    You can come back to the front launching from scratch or by coming back to life from suspension; it's the most basic fact of iOS app life! You just need to accept it.

    But there are lots of things you can do to reduce your chances of being terminated in the background. Giving up memory-consuming objects as you background is first on the list.