ios-Front and rear switch (life cycle during app run)

Source: Internet
Author: User

The lifecycle of an iOS application, and whether the program is running in the foreground or in the background, transforms the state of the application, which is important to the developer. The resources for iOS systems are limited, and applications are not the same in the foreground and in the background. In the background, the program is subject to many limitations of the system, which can improve battery usage and user experience.
To develop the app, we follow some of Apple's guiding principles, as follows:
1. Status of the application
The status is as follows:
Not running running program does not start
Inactive inactive The program runs in the foreground, but cannot accept event processing. When the app is switching from one state to another, the over-the-middle will briefly stay in this state.
The active activation program runs in the foreground and receives the event. This is the normal state at which the app is running in the foreground.
Backgroud Daemon is in the background and executing code. Most applications that will enter the Suspended state will briefly enter this state. If the app requests additional execution time, the app will remain in this state for a longer period of time. In addition, If an application needs to run directly into the background when it starts, such an application will go directly from the notrunning state to the Background state, and will not go through the Inactive state. For example, there is no interface to the application (of course, not specifically the application without the interface), in short, if the application directly into Background status, the application interface will not be displayed.
Suspended hangs the app in the background and does not execute any code. The system automatically transfers the app to that state without any notification. When in this state, the app still resides in memory, but does not execute any program code. When the system has a low memory warning, the system will be out of Suspended State is completely removed from memory, freeing up more memory for the foreground app.

is the program state change diagram:

Callback for the agent when each program is running state:

- (BOOL) Application: (uiapplication*) Application Willfinishlaunchingwithoptions: (nsdictionary*) launchoptions//tells the agent process to start but has not yet entered the state to save. The method is called automatically when the application is about to start, the first time the application executes custom code at startup.- (BOOL) Application: (uiapplication*) Application Didfinishlaunchingwithoptions: (nsdictionary*) launchoptions//Tell the agent to start the basic Completion program ready to start running. The method is called automatically when the application starts, and the developer can perform initialization of the associated code in the method.- (void) Applicationdidbecomeactive: (uiapplication*) Application//Application into active state execution. You can override this method to perform the final preparation when the application is transferred to the foreground, and when it enters the active state, it is called back when the application is invoked from the start to the foreground, or from the background to the foreground.- (void) Applicationwillresignactive: (uiapplication*) Application//When the application is going into an inactive state (the method will be called when the app is leaving the foreground run state), during which the application does not receive messages or events, such as a call- (void) Applicationdidenterbackground: (uiapplication*) ApplicationThe method is called when the program is in the Background state and is likely to enter the suspended state at any time. So to set the background to continue running, you can set it in this function- (void) Applicationwillenterforeground: (uiapplication*) Applicationthe method will be called when the program is going back to the foreground from the background but has not yet reached the active state. This is just the opposite of the method above. - (void) Applicationwillterminate: (uiapplication*) ApplicationThe method is called when the application is about to be terminated, and this method will not be called if the app is currently in the Suspended State: It is usually used to save data and some cleanup before exiting. This needs to set the key value of the uiapplicationexitsonsuspend. - (void) Applicationdidfinishlaunching: (uiapplication*) application executed when the program is loaded

Type NSLog print in the method that corresponds to the above 8 methods.
Now start the program to see the order of execution:
Start the program
LIFECYCLE[40428:11303] Willfinishlaunchingwithoptions
LIFECYCLE[40428:11303] Didfinishlaunchingwithoptions
LIFECYCLE[40428:11303] Applicationdidbecomeactive
Press the Home button
LIFECYCLE[40428:11303] Applicationwillresignactive
LIFECYCLE[40428:11303] Applicationdidenterbackground
Double-click the Home button and then open the program
LIFECYCLE[40428:11303] Applicationwillenterforeground
LIFECYCLE[40428:11303] Applicationdidbecomeactive
2. Application Life cycle
2.1. Loading the application into the foreground

ios-Front and rear switch (life cycle during app run)

Related Article

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.