Lesson 3. Application Lifecycle
This lesson explains the lifecycle of some components of an application. When developing an application is important to be aware of the interaction that the final user will have with it. Different events have an specific result in the application behavior, things like closing the application or losing the internet connection
|Expected Duration||30 minutes|
|Objective||• Comprehend the human-device relationship and the behavior of the application as a result of this relation.
• Implement some services for each state of the lifecycle of iOS.
• Learn to use some graphical components like load dialogs.
1. Lifecycle of an application
There are shared resources in the mobile devices like the battery and the memory RAM. That means that multiple processes in the device are constantly using those resources, and someone or something has to be in charge of managing them. when using a device with iOS, the OS in the one that is releasing (ending) processes that are not in use or to creating processes when needed. So, as developers we should be aware when are the processes of our app are in a state in which the OS is capable of releasing them.
The different stages and the changes between them of the components/processes/views of our application is called the lifecycle. The lifecycle of an iOS application can be represented in a general level with the following diagram:
Diagram 1: iOS lifecycle. Image from: https://developer.apple.com
The states of the lifecycle are:
Table 1: States of iOS lifecycle. Image from: https://developer.apple.com
In iOS the applications go to the Background state immediately after they leave the screen. But even if they stay only a short time in this state before they are suspended, there is a possibility that the application will continue to execute code in this state for much longer. For instance Waze, or Skype. Those kind of applications need special permissions to not be suspended by the system when they are in background state. In iOS there is a class called AppDelegate, that is in charge of implementing the methods that are launched when a change of state occurs or when other types of important changes happen, like notifications. The methods of the state changes are:
- application:willFinishLaunchingWithOptions: This is the first opportunity to execute code in the application when it is launched. Usually here is where you configure libraries and singleton instances.
- application:didFinishLaunchingWithOptions: This method allows you to complete your app initialization, before the application is shown to the user.
- applicationDidBecomeActive: This method is called to let your app know that it moved from the inactive to active state.
- applicationWillResignActive: This method is called to let your app know that it is about to move from the active to inactive state.
- applicationDidEnterBackground: This method is launched when the application is on background and can be suspended at any moment.
- applicationWillEnterForeground: This method is called as part of the transition from the background to the active state.
- applicationWillTerminate: This method lets your app know that it is about to be terminated and purged from memory entirely.
In each of this methods you can implement a lot of functionalities and actions. For instance, delete resources that the application will not need anymore when it goes to the background state, or created resources when the application comes back from the background state.
2. Lifecycle of a controller (ViewController)
We talked about the applications lifecycle, but the controllers have also one, with its respective states. The states are similar to the application ones and let the developer know when the view has been shown and when is not. The states are:
Diagram 2: transitions of state of a view controller. Image from: https://developer.apple.com
Like the AppDelegate methods, the controller allows you to implement methods when the state changes happens. The following list contains the methods in execution order:
|1.0||Juan David Cruz Serrano||February 20, 2017|
|1.1||Mario Linares||February 21, 2017|
|1.2||Juan David Cruz Serrano||February 21, 2017|
|2.0||Sergio Velasquez||February 28, 2018|