There and Back Again… When iOS Apps Go to The Background

Recently, I was trying to understand what happens when an iOS app goes into the background and then re-emerges later in various ways. It actually wasn’t as straightforward as I thought it would be. This may have already be documented somewhere, but it was a useful educational exercise nonetheless.

The driving reason for exploring the sequence was that I needed to suspend an NSTimer-based process when the app went into the background, and then start it back up when the app returned from suspended animation. Additionally, I needed to refresh a list from a web service when the app emerged, and the issue is that I needed to have it occur just once.

I needed to discover the events that occur and in what order when an app is active in the foreground and the following things happen:

  • User presses Home button
  • User Locks the device

I was also interested in seeing what happens when the reverse scenarios happen:

  • User re-launches app when in background
  • User unlocks the device
  • Initial launch of the app

Turns out the sequence wasn’t quite what I was expecting — but it was very instructive. The following table illustrates my results. The numbers just indicate sequence of events (what happened first and second).

Will Resign Active Did Become Active Did Enter Background Will Enter Foreground
App in FG + Home Button 1 2
App in FG + Lock 1
App in BG + Relaunch 2 1
App in FG + Unlock (after locking) 1
Initial Launch 1

The good thing about creating this table is that it was very helpful in activating and deactivating the NSTimer-based process and also triggering the list refresh when the application submerged and then emerged.

Let me know if anyone is interested in code for this…