Application States and Transitions

In Homepwner, the items are archived when the application enters the background state. It is useful to understand all of the states an application can be in, what causes them to transition between states, and how your code can be notified of these transitions. This information is summarized in Figure 18.6.

Figure 18.6  States of typical application

States of typical application

When an application is not running, it is in the not running state, and it does not execute any code or have any memory reserved in RAM.

After the user launches an application, it enters the active state. When in the active state, an application’s interface is on the screen, it is accepting events, and its code is handling those events.

While in the active state, an application can be temporarily interrupted by a system event like an SMS message, push notification, phone call, or alarm. An overlay will appear on top of your application to handle this event. This state is known as the inactive state. In the inactive state, an application is mostly visible (an alert view will appear and obscure part of the interface) and is executing code, but it is not receiving events. Applications typically spend very little time in the inactive state. You can force an active application into the inactive state by pressing the Lock button at the top of the device. The application will stay inactive until the device is unlocked.

When the user presses the Home button or switches to another application in some other way, the application enters the background state. (Actually, it spends a brief moment in the inactive state before transitioning to the background.) In the background, an application’s interface is not visible or receiving events, but it can still execute code. By default, an application that enters the background state has about ten seconds before it enters the suspended state. Your application should not rely on this number; instead it should save user data and release any shared resources as quickly as possible.

An application in the suspended state cannot execute code, you cannot see its interface, and any resources it does not need while suspended are destroyed. A suspended application is essentially freeze-dried and can be quickly thawed when the user relaunches it. Table 18.1 summarizes the characteristics of the different application states.

Table 18.1  Application states

State Visible Receives Events Executes Code
Not Running No No No
Active Yes Yes Yes
Inactive Mostly No Yes
Background No No Yes
Suspended No No No

You can see what applications are in the background or suspended by double-clicking the Home button to get to the multitasking display. (Recently run applications that have been terminated may also appear in this display.)

Figure 18.7  Background and suspended applications in the multitasking display

Background and suspended applications in the multitasking display

An application in the suspended state will remain in that state as long as there is adequate system memory. When the operating system decides memory is getting low, it terminates suspended applications as needed. A suspended application gets no notification that it is about to be terminated; it is simply removed from memory. (An application may remain in the multitasking display after it has been terminated, but it will have to be relaunched when tapped.)

When an application changes its state, the application delegate is sent a message. Here are some of the messages from the UIApplicationDelegate protocol that announce application state transitions. (These are also shown in Figure 18.6.)

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​
 ​ ​ ​ ​ ​ ​ ​ ​d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​o​p​t​i​o​n​s​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​D​i​d​B​e​c​o​m​e​A​c​t​i​v​e​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​;​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​W​i​l​l​R​e​s​i​g​n​A​c​t​i​v​e​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​;​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​D​i​d​E​n​t​e​r​B​a​c​k​g​r​o​u​n​d​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​;​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​W​i​l​l​E​n​t​e​r​F​o​r​e​g​r​o​u​n​d​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​;​

You can implement code in these methods to take the appropriate actions for your application. Transitioning to the background state is a good place to save any outstanding changes and the state of the application, because it is the last time your application can execute code before it enters the suspended state. Once in the suspended state, an application can be terminated at the whim of the operating system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset