iOS app life cycle

Source: Internet
Author: User

As a delegate object for an application, the Appdelegate class will callback different methods at different stages of the application life cycle. First, let's look at the different states of iOS apps and how they relate to each other, as shown in Figure 1.

Figure 1 iOS App status diagram

Here's a brief introduction to the 5 states of iOS apps.
? Not Running (non-running state). The app is not running or terminated by the system.
? Inactive (reception is not active). The app is entering the foreground state, but it cannot accept event processing.
? Active (foreground activity status). The application enters the foreground state and can accept event processing.
? Background (background state). The app is still able to execute code after it enters the background. If there is executable code, the code executes, and if there is no executable code or the executable code is executed, the application will go into the pending state immediately.
? Suspended (hang State). A pending application enters a "frozen" state and cannot execute code. If the system does not have enough memory, the application is terminated.
During the app state transitions, the IOS system will callback some of the methods in Appdelegate and send some notifications. In fact, there are a number of methods and notifications used in the life cycle of an application, and we have selected several major methods and notifications in detail, as described in Table 1.

Table 1 methods and local notifications for applying callbacks during state transitions

To make it easier to observe the running state of the application, we add some log output for the method in APPDELEGATE.M, with the following code:

@implementation appdelegate   -(BOOL) Application: (UIApplication *) Application      Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions {     NSLog (@ "%@", @ "application: Didfinishlaunchingwithoptions: ");     return YES; }-(void) Applicationwillresignactive: (uiapplication *) application {         NSLog (@ "%@", @ "applicationwillresignactive :"); } -(void) Applicationdidenterbackground: (uiapplication *) application {         NSLog (@ "%@", @ " Applicationdidenterbackground: ");} -(void) Applicationwillenterforeground: (uiapplication *) application {         NSLog (@ "%@", @ " Applicationwillenterforeground: ");} -(void) Applicationdidbecomeactive: (uiapplication *) application {         NSLog (@ "%@", @ "applicationdidbecomeactive:") ; }-(void) Applicationwillterminate: (uiapplication *) Application {         

In order to make people more intuitive understanding of the State and its corresponding methods, notification of the relationship between the following, we take a few application scenarios as the point of the system analysis.

(a) Non-operational status-application launch scenario

Scenario Description: When the user clicks on the app icon, it may be the first time the app is launched, or it may be started again after the app terminates. The state transition process for this scenario is shown in Figure 2, with a total of two stages of 3 states: not running→inactive→active.
? In the not running→inactive phase. Call Application:didfinishlaunchingwithoptions: Method to issue a uiapplicationdidfinishlaunchingnotification notification.
? In the inactive→active phase. Call Applicationdidbecomeactive: Method to issue a uiapplicationdidbecomeactivenotification notification.

Figure 2 Applying the state transition process for a startup scenario

(b) Click the Home button-app exit scenario

Scenario Description: When the app is running (that is, the active state), clicking the Home button or having another app causes the current app to break. The state transition process for this scenario can be divided into two situations: it can be run in the background or suspended, not in the background or suspended. Depending on the properties in the product properties file (such as helloworld-info.plist) application does not run in background can control both states. If you open the Helloworldinfo.plist file with a text editor, the key for the setting entry is uiapplicationexitsonsuspend.

The first case of a state transition: The application can be run in the background or suspended, the state transition process of the scene is shown in Figure 3, a total of 3 stages 4 states: Active→inactive→background→suspended.

? In the active→inactive phase. Call Applicationwillresignactive: Method to issue a uiapplicationwillresignactivenotification notification.
? In the Inactive→background phase. Apps go from inactive to backstage (without involving the methods and notifications we want to focus on).
? In the background→suspended phase. Call Applicationdidenterbackground: Method to issue a uiapplicationdidenterbackgroundnotification notification.

               

Figure 3 Click the Home button to apply the exit scenario

State transitions in the second case: the application can not be run in the background or hang, and its status transitions are shown in Figure 4, a total of 4 stages 5 states: Active→inactive→background→suspended→not running.

? In the ACTIVE→INACTIVD phase. The app is moved from active to inactive (does not involve methods and notifications that we want to focus on).
? In the Inactive→background phase. Apps go from inactive to backstage (without involving the methods and notifications we want to focus on).
? In the background→suspended phase. Call Applicationdidenterbackground: Method to issue a uiapplicationdidenterbackgroundnotification notification.
? In the Suspended→not running stage. Call Applicationwillterminate: Method to issue a uiapplicationwillterminatenotification notification.

Figure 4 Click the Home button to apply the exit scenario

iOS does not support multitasking before iOS 4, when you click the Home button, the app exits and breaks, and after iOS 4 (including iOS 4), the operating system can support multitasking, and clicking the Home key app will go backstage without interruption (except in the case of insufficient memory).
The application can also be partially processed in the background, and the processing will go into a pending state.

(iii) Suspend re-run scenario

Scenario Description: The suspended state of the app is rerun. The state transition process of the scene is shown in 5, with a total of 3 stages of 4 states: Suspended→background→inactive→active.

Figure 5 The state transition process for re-running the scene

? Suspended→background stage. The app goes backstage from the suspended state (it doesn't involve the methods and notifications we're talking about).
? Background→inactive stage. Call Applicationwillenterforeground: Method to issue a uiapplicationwillenterforegroundnotification notification.
? Inactive→active stage. Call Applicationdidbecomeactive: Method to issue a uiapplicationdidbecomeactivenotification notification.

(d) Memory removal-application termination scenario

Scenario Description: The app enters a pending state when the background processing is complete (this is a dormant state), and if a low memory warning is issued, the application will be purged of memory to terminate the operation in order to meet the memory needs of other applications, and the state transitions of the scene are shown in Figure 6.

                      

Figure 6 Memory Cleanup termination scenario

When the memory clears, the application terminates the operation. There are two cases of memory cleanup, either the system forcing the memory to clear or the user manually clearing it from the taskbar (that is, deleting the app). After the memory clears, if the app runs again, the last run state will not be saved, equivalent to the first run of the app.

In a memory purge scenario, the app does not call any methods and does not issue any notifications.

iOS app life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.