applicationWillTerminate vs. applicationWillResignActive vs. applicationDidEnterBackground? When are they triggered exactly?

Viewed 480

I know there exists a similar question but I think that question is a lot more general and does not answer on the specific details that I'm trying to find out.

I've read the Apple documents, but still I'm a bit confused on when exactly these methods are triggered. From the Apple documents, it seems there are the following state transitions that are possible for an iOS app:

  1. App terminated in the foreground (for example double tap the Home button and swipe up the app): pretty sure applicationWillTerminate is triggered in this case, actually this is the only case that I'm not too confused about.

  2. App switched to the background while still running in the background (for "apps that support background execution" to quote Apple documents): applicationDidEnterBackground is triggered? applicationWillResignActive is triggered or not?

  3. Background running App gets terminated: applicationWillTerminate may(?) be triggered according to the Apple documents... so it may or may not be triggered at times?

  4. App switched to the background and immediately suspended: applicationDidEnterBackground and applicationWillResignActive are both triggered?

  5. Background running App gets suspended: applicationWillResignActive is triggered? Or nothing gets triggered?

  6. App that "does not support background execution" gets switched to the background: applicationWillTerminate gets triggered according to Apple documents? But what exactly does this actually talk about?

  7. App crashes in the foreground: nothing gets triggered in this case I guess?

  8. Background running App crashes: also nothing gets triggered in this case right?

  9. Background suspended App gets terminated: Again nothing gets triggered I guess?

I think the Apple documents are either somewhat vague or downright confusing in explaining those methods. Sometimes they talk about "apps that support background execution" vs. "apps that do not support background execution", sometimes they talk about "running in the background" vs. "suspended", sometimes they just say things like "the user quits the app and it begins the transition to the background state" or "incoming phone call or SMS message", which are real-world use-cases instead of technical concepts.

So anyone can help explain in more consistent technical terms that when exactly are those methods triggered in specific cases?

EDIT: Not sure why this question is deemed not "focused", as I'm trying to make use of those application delegate methods but after both some personal testing and reading official documents, I'm still at a loss when exactly those methods will be triggered and how may I use them reliably.

Maybe I can elaborate more by borrowing some stuff from PGDev's answer, so there are 5 states an iOS app can be in if I understand correctly:

  1. Not running
  2. Inactive
  3. Active
  4. Suspend
  5. Background

And when the app changes state, some delegate methods may be triggered, but so far from the official Apple documents, it seems very confusing which methods will be triggered during which state transition.

For example I'm not sure what or if any method will be triggered when app goes from state 5 to state 4, and when app goes from state 5 to state 1, and when app goes from state 4 to state 1. Also from the official Apple documents it seems to say that applicationWillTerminate will be triggered if "apps that do not support background execution" are switched to the background, however I have not been able to get any kind of app to trigger applicationWillTerminate by just pressing the Home button.

As it currently stands, I'm not sure if I can reliably utilize those methods to do anything, that's why I hope someone can give a clearer picture on when exactly those methods will be triggered (and not triggered) apart from those vague descriptions in the official Apple documents.

1 Answers

Here is the detailed explanation of how the App Lifecycle methods work.

There exist 5 states in the the lifecycle of the app,

  1. Not running — The app is not running.

  2. Inactive — running in foreground + not receiving events

    • An iOS app can be placed into an inactive state, for example, when a call or SMS message is received.(Foreground + Not Running)
  3. Active — running in foreground + receiving events

  4. Suspend — running in background + not executing code

  5. Background — running in background + executing code

Next there exist 7 delegate methods that are called whenever the above states are changed in the app's session.

Launch time

  1. application:willFinishLaunchingWithOptions

    • Method called when the launch process is initiated.
    • This is the first opportunity to execute any code within the app.
    • launch process has begun but that state restoration has not yet occurred.
  2. didFinishLaunchingWithOptions

    • Method called when the launch process is almost done and the app is almost ready to run. Final initialization.

Transitioning to the foreground

  1. applicationDidBecomeActive
    • app has become active.

Transitioning to the background

  1. applicationDidEnterBackground
    • This method is called when an iOS app is running, but no longer in the foreground

Transitioning to the inactive state:

  1. applicationWillResignActive

    • exit from active state to another state
    • Each time a temporary event, such as a phone call, happens this method gets called
    • called when app moves to background
  2. applicationWillEnterForeground

    • about to enter from background -> foreground

Termination

  1. applicationWillTerminate
    • app is about to terminate.
    • Force quitting the iOS app, or shutting down the device
    • This is the opportunity to save the application configuration, settings, and user preferences.

Some flow examples of the order in which the above methods will be called.

  • Launch app — 1, 2, 3 — willLaunch + didLaunch + didBecomeActive
  • Press home — 5, 4 — resignActive + didEnterBackground
  • Open app from background — 6, 3 — willEnterForeground + didBecomeActive
  • Open task manager — 5 — resignActive
  • Phone call comes when app in foreground — 5 — resignActive

You can test the execution of the lfecycle methods using the above examples.

Related