Differences between applicationDidEnterBackground and applicationWillTermimate

In my application, when the user clicks the home button, I save the data in NSUserDefaults . The application runs in the background. When the user restarts the application, I use the applicationWillEnterForeground method to display the saved data.

However, when the user presses the home button twice and exits the application, choosing the minus sign in the application, I need to save different data in the same default users. But when I exit the application applicationWillTerminate , it is called sometimes, and sometimes not.

So, how can I tell if the application is just minimized or is it terminated?

+7
source share
6 answers

Whenever the user selects once, and the application will run in the background at this time, applicationDidEnterBackground always calls. In this method, you can temporarily update / save the value of NSUserDefaults.

If in case the application reappears in the foreground with termination with double taps, it calls applicationDidBecomeActive , in which you can update / delete the temporarily saved NSUserDefaults value again. And if the user closes the application with double labels, the stored value will be stored in NSUserDefaults.

+7
source

For the difference between UIApplicationDelegate methods UIApplicationDelegate see the UIApplicationDelegate Protocol Reference . applicationDidEnterBackground: will always be called when the home button is pressed. But it seems that applicationWillTerminate: cannot be called.

If your application does not work in the foreground, it can be either in the background or in standby mode. Please refer to the “Launching the Application” section of the Application Programming Guide for iOS — Application Status and Multitasking .

When an application runs in the background and monitors events, applicationWillTerminate: is called when it exits. When the application is paused, it will not. See the description in the Termination of Application section:

Even if you are developing your application using the iOS SDK 4 and later , you should still be prepared for your application to be killed without notice. The user can explicitly kill applications using the multitasking interface. In addition, if memory is limited, the system can remove applications from memory to create more space. Suspended applications are not notified of completion, but if your application is currently running in the background (and not pausing), the system calls the applicationWillTerminate: method of your application delegate. Your application cannot request additional background runtime from this method.

+5
source

applicationDidEnterBackground . When the user presses the home button and the application will run in the background, then it calls the applicationDidEnterBackground method. It is called when the user minimizes the application by clicking the Home button or switching to another application.

applicationWillTerminate . It is called only when the application process is actually killed. That is, the user kills him using the red close minus button in the task switch, or the system itself kills him in the background (for example, with a low memory level). Your application cannot request additional background execution time from this method.

+1
source

applicationDidEnterBackground - this method is called when the user sets the application to the background by pressing the home button.

applicationWillTerminate - this method is called when the user leaves the application in the background by pressing the red minus button.

+1
source

applications that support background execution, this method is usually not called when the user exits the application, because the application simply moves to the background in this case

If there is data that needs to be saved, what should you do when the application goes into the background.

0
source

applicationWillTerminate is called only when the application process is actually killed. That is, the user kills him with the red minus-close button in the task switch, or the system itself kills him in the background (for example, in low memory conditions).

For your needs, applicationDidEnterBackground preferable; It is called whenever the user minimizes the application by clicking the Home button or switching to another application.

0
source

All Articles