An explanation of the iOS interface life cycle process

Source: Internet
Author: User

People who have developed Android know that every Android interface is an activity, and each activity has its own life cycle, and there are a number of ways to control the activity's life cycle, such as: OnCreate (), OnStart (), Onresume (), OnDestroy () and so on. There is also this process control in iOS. This blog first discusses the control process for an iOS app.

In a newly created program there will be a Appdelegate.swift file containing a series of methods that embody the iOS control flow. Here is the system's own generated code, let's feel:

Import Uikit@uiapplicationmainclass Appdelegate:uiresponder, uiapplicationdelegate {var window:uiwindow? Func application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?)-        > Bool {//Override point for customization after application launch. return true} func applicationwillresignactive (application:uiapplication) {//Sent when the application is About-to-move from active-to-inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US        Er quits the application and it begins the transition to the background state. Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates.                    Games should use this method to pause the game. } func Applicationdidenterbackground (application:uiapplication) {//Use this method to release shared resource s, save user data,Invalidate timers, and store enough application, state information to restore your application        It is terminated later. If your application supports background execution, this method is called instead of Applicationwillterminate:when the                    User quits.  } func Applicationwillenterforeground (application:uiapplication) {//called as part of the transition from the Background to the inactive state;                   Here's can undo many of the changes made on entering the background. } func applicationdidbecomeactive (application:uiapplication) {//Restart any tasks, were paused (or not ye T started) while the application was inactive.                   If the application is previously in the background, optionally refresh the user interface. } func applicationwillterminate (application:uiapplication) {//Called when the application are about to Termina Te. Save data if appropriate. See also ApplicaTiondidenterbackground:. }}
The comments are also automatically generated by the code, so let's look at some of these methods in detail below.

(1) Func applicationwillresignactive (application:uiapplication)
This method is called when the current application is about to exit the active state, such as when the interface is about to be returned to the main interface. Can be compared with the OnPause () method in Android. The official explanation also said that the program temporarily stopped running, such as a phone call, or text messages will call the method.


(2) Func applicationdidenterbackground (application:uiapplication)

This method is typically called after applicationwillresignactive (), indicating that the app will run in the background. The official explanation also says that the program calls this method to release shared resources and save the user's data, as well as store information about the current application for use when the program resumes running. Note: If your application supports running in the background, when the user exits the program, the Applicationwillterminate () method is not called and the Applicationdidenterbackground () method is called instead.


(3) Func Applicationwillenterforeground (application:uiapplication)

This method is called when the application enters the active state (foreground). If it is running in the background, now click on the icon and rerun the program activity.


(4) Func applicationdidbecomeactive (application:uiapplication)
This method is typically called before the Applicationwillenterforeground () method, which indicates that the application is about to enter the foreground.


(5) Func applicationwillterminate (application:uiapplication)

This method is called when the program is terminated, such as when the current program is removed from the most recently run program.


Below we make a little change to the above code, output a sentence in each method, and do various operations in the simulator, observe the output results, you can know the entire application execution process.

Func applicationwillresignactive (application:uiapplication) {                println ("application would Resign Active")    }    func Applicationdidenterbackground (application:uiapplication) {                println ("Application did Enter Background" )    }    func Applicationwillenterforeground (application:uiapplication) {                        println ("Application'll Enter Foreground ")    }    func applicationdidbecomeactive (application:uiapplication) {                        println (" Application did Become Active ")    }    func applicationwillterminate (application:uiapplication) {                        println (" Application Would Terminate ")    }

(a) Start-Press the Home key

Execution result: application did become active-->application would Resign Active-->application did Enter background


(b) Launch--Press home--click the icon to start again

Execution result: application did become active-->application would Resign Active-->application did Enter background--> Application would Enter Foreground-->application did become Active


(iii) Start-up and remove from recent applications

Application did become active-->application would Resign Active-->application did Enter background--> Application'll Terminate


The above is a relatively simple operation control, there will be more complex later, we are going to study slowly.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An explanation of the iOS interface life cycle process

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.