IOS Application Life cycle

Source: Internet
Author: User

Status of the application
    • Not running (not running): program does not start
    • Inactive (not activated): The program runs in the foreground, but no events are received. In the absence of event handling, the program usually stays in this state
    • Active (Active): The program runs in the foreground and receives events. This is also a normal mode of the front desk
    • Backgroud (Backstage): Sequential in the background and can execute code, most of the program into the background will stay in this state for a while. The time will then go into the suspended state (Suspended). Some programs can be in Backgroud state for a long time after special request
    • Suspended (hangs): The program cannot execute code in the background. The system automatically turns the program into this state and does not give notice. When suspended, the program is still in memory, when the system memory is low, the system will remove the suspended program, to provide more memory for the foreground program

Entry function
int Main (intChar * argv[]) {    @autoreleasepool {        return  Class]));}    }

// If Nil is specified for Principalclassname, the value of Nsprincipalclass from the info.plist is used. If there is no//  nsprincipalclass key specified, the UIApplication class is used. The delegate class would be instantiated using init.  int uiapplicationmain (intchar *argv[], nsstring *principalclassname, NSString * Delegateclassname);

ARGC and argv are meant to be consistent with the C language, which is useless in this, not detailed.

The following two parameters are Principalclassname (primary class name) and Delegateclassname (delegate class name).

If Principalclassname is nil, then its value is taken from info.plist, and if not in Info.plist, the default is UIApplication. Principalclass This class does nothing except manage the life cycle of the entire program, it only assigns the value of the listener event and then gives it to Delegateclass to do it.

Delegateclass will instantiate an object when the project is new .

class])

Equivalent to @ "Xyzappdelegate".

Delegate class implementation file

Note the official comments in the code.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions{//Override point for customization after application launch.NSLog (@"didfinishlaunchingwithoptions"); returnYES;} - (void) Applicationwillresignactive: (UIApplication *) application{//Sent when the application are 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.NSLog (@"willresignactive");}- (void) Applicationdidenterbackground: (UIApplication *) application{//Use the method to release the shared resources, save user data, invalidate timers, and store enough application state in     Formation to the restore your application to the it is terminated later. //If Your application supports background execution, this method is called instead of Applicationwillterminate:when th E user quits.NSLog (@"Didenterbackground");}- (void) Applicationwillenterforeground: (UIApplication *) application{//Called as part of the transition from the background to the inactive state; Here you can undo many of the changes mad E on entering the background.NSLog (@"Willenterforeground");}- (void) Applicationdidbecomeactive: (UIApplication *) application{//Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.NSLog (@"didbecomeactive");}- (void) Applicationwillterminate: (UIApplication *) application{//Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.NSLog (@"willterminate");}
View Code

Start the program

2014-07-28 15:22:39.883 lifecycle[3024:a0b] Didfinishlaunchingwithoptions

2014-07-28 15:22:39.887 lifecycle[3024:a0b] Didbecomeactive

Press the Home button

2014-07-28 15:22:43.130 lifecycle[3024:a0b] Willresignactive

2014-07-28 15:22:43.131 lifecycle[3024:a0b] Didenterbackground

Re-click the program

2014-07-28 15:22:44.380 lifecycle[3024:a0b] Willenterforeground

2014-07-28 15:22:44.380 lifecycle[3024:a0b] Didbecomeactive

1.application:didfinishlaunchingwithoptions:

The program has been executed for the first time , and if it is started directly, there is no data in the launchoptions; otherwise, launchoptions will contain the content of the corresponding method (such as starting Tempo master from it).

2.applicationWillResignActive (will go backstage)

Called when the program is about to lose its active state, such as pressing the home key or having a phone message coming in. Corresponding Applicationwillenterforeground (will enter the foreground), this method is used to

    • (a) suspending the tasks under implementation;
    • no timer;
    • Reduce OpenGL es frame rate;
    • If the game should be suspended;

3.applicationDidEnterBackground (has entered the background)

The program has been called in the background, corresponding to the applicationdidbecomeactive (has become the foreground), this method is used to

    • releasing shared resources;
    • Save user data (write to hard disk);
    • void timer;
    • Save enough program state for the next recovery;

4.applicationWillEnterForeground (will enter the front desk)

The program is about to go into the foreground when called, corresponding to Applicationwillresignactive (will enter the background). This method is used to undo the changes made in applicationwillresignactive.

5.applicationDidBecomeActive (has entered the front desk)

Called when the program has become active (foreground). Corresponds to the Applicationdidenterbackground (has entered the background). If the program is in the background before, finally refresh the user interface within this method.

6.applicationWillTerminate

Called when the program is about to exit. Remember to save the data, like the Applicationdidenterbackground method.

Reference articles

iOS Learning Notes (iv)--ios application life cycle

An explanation of the iOS app lifecycle (front background switch, application status)

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.