iOS Application life cycle
It’s most basic yet often neglected topic in iOS world.
I see so many experienced developer not putting much emphasis on application life cycle and creating known mistakes in app development.
Agenda of this article - To cover basics of application life cycle and then a deep dive into real world scenarios to help you ace your interviews
What is application life cycle - It constitutes the sequence of events that occurs between the launch and termination of application
Why do we care - With its knowledge one can make enriched , immersive and smooth User experience
At any given moment, all interaction happens in between 5 states. These are
- Not running
- Inactive
- Active
- Background
- Suspended
Let’s understand what these jargons are
1) Not running state - Happens when
- App is not launched yet
- App was running in background but is terminated by system
2) Inactive state - Happens when App is running in foreground but is not receiving events
- Generally it remain for brief time in this state before it make transition to other state
- Makes transition into this state when call or message is received
- Can’t interact with UI
3) Active state - An application is running in the Foreground and receiving the events
- All transition to this state comes from Inactive state
- User can interact with app UI
4) Background State - An application is running in the background and executing the code.
- In this state, if there is executable code, it will execute and if there is no executable code or the execution is complete, the application will be suspended immediately
5) Suspended state - The app is in the background(in memory) but is not executing code
- The system moves the application to this state automatically and does not notify
- In case of low memory, the system may purge suspended application without notice to make free space for the foreground application.
- Usually after 5–6 secs spent in the background, apps will transition to Suspend state, but we can extend the time if app needs.
What happens when user taps on app icon
When user tap on your app icon, SpringBoard launches your app.SpringBoard is the standard application that manages the iPhone’s home screen. As springboard animates your app’s launch screen, your app and necessary shared libraries will be loaded into the memory and application begins its execution and AppDelegate receives notification
ApplicationDelegate
- Main entry into iOS apps is UIApplicationDelegate.
- It is a protocol and you need to implement that into your app to get notified about user events such as app launch, app goes into background or foreground, app is terminated, a push notification was opened etc.
- All the method that gets notified about various app states comes within this set of protocol.
- Let’s see the some application life cycle method and how they gets called.
Top Interview questions on Application life cycle
- What is the class hierarchy of AppDelegate class?
AppDelegate ->UIResponder -> NSObject
2. How can you opt out background execution?
Can explicitly opt out background execution by adding UIApplicationExitsOnSuspend
key to application’s Info.plist
file and setting its value to YES
.
3. In which app revolves in when we opt out for background execution
App life cycles will be between the not running, inactive, and active states and never enters the background or suspended states.
4. What are use case of background state
- Gives time to save user data that may be required in next session
- Release resources that are not needed by app