Swift App states — The App Life Cycle

How are you going to respond to notifications if you don’t know the state your App is in?

We need to be aware of the execution states for Apps, because by definition a user can remove our App from the list of currently open Apps at any time!

Difficulty: Easy | Normal | Challenging

Prerequisites:

  • Some understanding of how mobile Apps are used

Terminology

At any time an App is in one of 5 states

Not running: Could have just been installed!

Inactive: Open, but not receiving events, could be as it is transitioning to other states

Active: The normal mode of being in the foreground and recieving events

Background: Not visible but can execute code. This is useful when we are on the way to being suspended — so can send an API call (if you are lucky!)

Suspended: In the background but not executing code. Can be totally killed at any time (if there is low memory, for example).

Receiving events

You can receive events in some of the states, but not for others.

Image for post
Image for post

The transitions

After launch, the system puts the App into the inactive state if the App is in the process of displaying the UI on the screen. While in this state the App is unable to receive notifications. This makes sense, as we move from not running we transition to active through an inactive state and it would not make sense to get notifications during these transactions (as it: we cannot receive events while the App is in one of the inactive transition states).

The same is true when we move to or from the background state to the active state, in every case we pass through an inactive state as the App is “woken up” so to speak.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store