The life cycle of the application is the different state of the program in the foreground and backstage and on the way to the backstage and to the foreground, these different states are controlled by different functions, of course, each function is different, for example, you may need to pause the program when you are in the background, you need to save some data in order to recover again. You will need to restore some data to enter the foreground, you may need to refresh the user interface and so on. As for the details, there is an English explanation in each paragraph, and patience can be seen.
How to test it? The idea is that when we simulate entering and exiting a program, the system calls these functions (of course there are no content in them), and we can let these functions call out their names so that we can know which function is called when we do something. _ cmd represents the current function. So add a print current function to each of the following functions:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {NSLog (@ "%@", Nsstringfromselector (_cmd)); Override point for customization after application launch. return YES;} -(void) Applicationwillresignactive: (uiapplication *) application {NSLog (@ "%@", Nsstringfromselector (_cmd)); 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 with this method to pause the game.} -(void) Applicationdidenterbackground: (uiapplication *) application {NSLog (@ "%@", Nsstringfromselector (_cmd)); Use the method to release shared resources, save user data, invalidate timers, and store enough application state information to the restore your application to their current state in case it is terminated later. If your application supports background execution, this method is called instead of Applicationwillterminate:when the User quits.} -(void) Applicationwillenterforeground: (uiapplication *) application {NSLog (@ "%@", Nsstringfromselector (_cmd)); 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.} -(void) Applicationdidbecomeactive: (uiapplication *) application {NSLog (@ "%@", Nsstringfromselector (_cmd)); 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.} -(void) Applicationwillterminate: (uiapplication *) application {NSLog (@ "%@", Nsstringfromselector (_cmd)); Called when the application are about to terminate. Save data if appropriate.See also Applicationdidenterbackground:.} -(void) applicationdidreceivememorywarning: (uiapplication *) application{NSLog (@ "%@", Nsstringfromselector (_cmd));}
When we press CMD+R to test the application, we go directly into the program, and this time we find that we have called 2 functions, the first of course is didfinishlaunchingwithoptions, because it is the program entrance, The second is applicationdidbecomeactive.
Then we press cmd+shift+home into the main interface to discover that 2 functions, applicationwillresignactive and Applicationdidenterbackground, were called. Click into application discovery also called 2 functions, Applicationwillenterforeground and applicationdidbecomeactive. This is the 4 functions we use.
Press CMD+SHIFT+M to simulate the memory warning found calling applicationdidreceivememorywarning (this function is not in the template created by default, we can add it manually).
But Applicationwillterminate has not been called, because this function is very useful only before iOS4.0, because it is not possible to multitask at that time, and exiting a program means closing the termination. But now even though we're out of a program, it's still not closed in the background, so this function is used less. If it's really going to work, we can add an option to the info.plist (meaning not to run in the background, which is to shut down in the background):
At this point in the background, we found that 3 functions were called, many of which applicationwillterminate this function.
Good habit: After the test remember to add the above info.plist option to delete Oh!
"iOS Dev-4" uiapplication life cycle and how to simply test when each function is called?