Why does viewWillAppear not get called when an app comes back from the background?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the iOS development lifecycle, viewWillAppear(_:) is a method commonly used within a UIViewController to perform tasks before a view becomes visible to the user. Under seemingly straightforward scenarios, such as transitioning from one view controller to another, it consistently gets called. However, there can be confusion as to why this method does not trigger when an application returns from the background state. This article delves into the technical reasons and nuances behind this behavior and offers a comprehensive understanding of view controller lifecycle events in relation to app state transitions.
Understanding viewWillAppear(_:)
Functionality
The viewWillAppear(_:) method is part of the view controller lifecycle and is typically used to update the UI with any important data just before it becomes visible on screen. For instance, developers might refresh the user interface or initiate network requests in this method to ensure everything is up-to-date when a view is presented.
Typical Use Cases
- Refreshing data or UI components.
- Starting animations that should occur every time the view appears.
- Adjusting the view layout based on new data or device orientation.
- Logging analytics or debugging information.
App Life Cycle vs. View Controller Life Cycle
App Life Cycle
The app life cycle in iOS consists of several states:
- Not Running: The app is not launched or has been terminated.
- Inactive: The app is in the foreground but not receiving events (e.g., user receives a call).
- Active: The app is in the foreground and receiving events.
- Background: The app is in the background and executing code.
- Suspended: The app is in the background, not executing any code.
View Controller Life Cycle
Conversely, the UIViewController lifecycle involves:
viewDidLoad()viewWillAppear(_:)viewDidAppear(_:)viewWillDisappear(_:)viewDidDisappear(_:)
These methods correspond to the stages of how a view controller’s view is presented on the screen, updated, and then removed.
Why viewWillAppear(_:) Does Not Get Called
Application in Background
The key reason viewWillAppear(_:) is not invoked when an app returns from the background lies in the distinction between the app lifecycle and view controller lifecycle. When an app transitions from the foreground to the background and back again:
- The view controller remains loaded into memory.
- The view itself might still be attached to the view hierarchy.
- There is no need for the system to reload the view or redraw UI components, as they have merely been hidden, not destroyed.
Thus, the system does not call viewWillAppear(_:) again, since technically, the view is already appearing.
Relevant Lifecycle Method
Instead of viewWillAppear(_:), what developers should handle to manage transitions that involve returning from the background is:
applicationWillEnterForeground(_:): Triggered just before the app enters the foreground. Ideal for tasks like refreshing data that doesn’t involve altering UI components directly.applicationDidBecomeActive(_:): Called when the app transitions from the inactive to the active state. Suitable for restarting tasks paused (or not yet started) while the app was inactive.
Practical Solutions
Use Notification Center
Developers can use the NotificationCenter to add observers for application state changes to trigger relevant logic manually. For instance, listening for UIApplication.willEnterForegroundNotification could allow you to manually trigger updates similar to those you might place in viewWillAppear(_:).
Summary Table
Below is a table summarizing key differences and behaviors between the app lifecycle and view controller lifecycle methods in relation to the app returning from the background:
| Aspect | App Lifecycle Method | View Lifecycle Method |
| State when returning | applicationWillEnterForeground(_:)
applicationDidBecomeActive(_:) | viewWillAppear(_:) is not called |
| Typical use cases | Refresh data, restart paused tasks Update non-UI logic | Update visible UI, invoke animations |
| Transition type | App state transition (background to foreground) | View state transition (e.g., transitioning from hidden to visible) |
| Response strategies | Use NotificationCenter
Manual triggers | System automatic call (under appropriate view transitions) |
By differentiating between app lifecycle and view controller lifecycle, and leveraging the specific events and mechanisms provided by iOS, developers can ensure their application reacts appropriately to changes in state and behaves reliably from the user's perspective. Understanding these nuances will help you build more resilient and user-friendly applications.

