Skip to content
tezvyn:

iOS App States: From Launch to Suspension

Source: developer.apple.comEasyHow cards are made

iOS App States: From Launch to Suspension

An iOS app is like a person with different states of alertness: focused (Active), distracted (Inactive), or napping (Background/Suspended). This lifecycle dictates how your app behaves during interruptions.

Why it exists

iOS is a resource-constrained environment, especially for battery and memory. The app lifecycle model exists to ensure a responsive user experience and preserve battery life by strictly managing what apps can do and when, especially when they aren't the frontmost app.

The mental model

Think of your app's state like a person's level of consciousness. NOT RUNNING is asleep and unaware. INACTIVE is awake but distracted, like when a phone call comes in over your app. ACTIVE is fully engaged with the user. BACKGROUND is not on-screen but can still perform short, finite tasks. SUSPENDED is in memory but completely frozen, like being in cryo-stasis, executing no code but ready to be thawed quickly.

How it works

When a user launches your app, it moves from NOT RUNNING to INACTIVE and then to ACTIVE. If a system alert appears, it transitions to INACTIVE. When the user presses the Home button, it moves to BACKGROUND. Here, it gets a short window of time to finish crucial tasks, like saving user data. After that, the system moves it to the SUSPENDED state. A suspended app stays in memory but executes no code. If the system experiences memory pressure, it may purge suspended apps to free up resources, moving them back to NOT RUNNING without any further notification.

When to use it

You don't choose a state, you react to state transitions. Your app's delegate object (AppDelegate or SceneDelegate) receives callbacks for these changes, like applicationDidEnterBackground. You use these callbacks to save user data, pause games, stop timers, and release large memory resources before the app is suspended. This prevents data loss and makes your app a good citizen of the OS.

When not to use it

Don't try to fight the system. Avoid attempting to run long, continuous tasks in the background unless you have explicitly registered for a specific background execution mode (like audio playback or location updates). Trying to trick the system into letting your app run indefinitely in the background will cause it to be terminated by the system's watchdog process.

One canonical example

A user is editing a document in a text editor app (ACTIVE state). They swipe to go to the Home Screen. The app gets a sceneDidEnterBackground notification. In response, the app's code immediately saves the current text to a file on disk. The app is then moved to SUSPENDED. Later, if the system terminates the app due to low memory, the user's work is safe. When they relaunch the app, it can load the document from the saved file.

Interview question

What is the primary reason iOS transitions an app from the BACKGROUND to the SUSPENDED state?

  • a.To free up CPU cycles and battery while retaining the app's state in memory for a quick relaunch.Correct
  • b.To provide the user with a notification that the app is no longer actively running.
  • c.To allow the app to continue executing long-running background tasks without interruption.
  • d.To immediately terminate the app and reclaim all system resources, including memory.
Why?

The SUSPENDED state freezes the app in memory, stopping all code execution to conserve CPU and battery, yet allows for a fast return to the ACTIVE state. It does not allow for continued background tasks, nor does it immediately terminate the app.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles